如何在C中发出PID信号

时间:2017-12-10 23:49:22

标签: c

对我来说,C对我来说太新了。我有两个可执行文件,一个是writeP,另一个是readP。 WriteP可执行文件写入共享数组并读取,但readP等待写入完成它的任务。

如何执行;假设执行如下。 WriteP Executable写入10个数组并等待信号。 ReadP,坐在忙碌中等待写入发出信号。一旦给出信号,它就会读取10个数组并打印该值。我的工作非常完美。现在我想再添加一次迭代。

WriteP,写入10个数组并等待。 ReadP,读取10个数组并向writeP发送信号以写入下10个数组。 ReadP将坐下来等待信号。

请问我的代码怎么不会迭代接下来的10个数组呢?

readP.c

int main(int argc, char *argv[])  //expects pid of writer as clarg
{
    int i, *pint, *pint1, shmid;
    int pid = atoi(argv[1]);

    shmid = shmget(SHMKEY, 128, 0666);
    pint = (int*) shmat(shmid, 0, 0);

    /*busy wait until writer sets the flag to indicate writer finished writing*/
    while(*pint != -1);

    for(i = 0; i < 10; i++)
            printf("%d\n", *pint++); /* display sh. mem */

    kill(pid, 10); /* send signal to shwrite that's OK to remove shmem */
    printf("signal just sent \n");

    /*busy wait until writer sets the flag to indicate writer finished writing*/
    while(*pint != -1) ;


    for(i = 10; i < 20; i++)
            printf("%d\n", *pint++); /* display sh. mem */

    kill(pid, 10); /* send signal to shwrite that's OK to remove shmem */
}

writeP.c

int main (void)
{
    int *pint, *ppint, i;



    /* force any signal from 1-20 sent to this process to call handler
    cleanup, which will detach shared mem before exiting */
    for (i = 0; i < 20; i++)
            signal(i, cleanup);

    shmid = shmget(SHMKEY, 128, 0666|IPC_CREAT);
    pint = (int*) shmat(shmid, 0, 0);
    ppint = pint;

    for(i = 0; i < 10; i++)
            *ppint++ = i; /* load sh. mem with ints 0-10 */
    *pint = -1;  /* load -1 into start of shmem */


    printf("First break point");

    pause(); /* wait here until a signal occurs--eg, kill from reader */

    printf("Second break point");

    for(i = 10; i < 20; i++)
            *ppint++ = i; /* load sh. mem with ints 0-10 */
    *pint = -1;  /* load -1 into start of shmem */

    pause(); /* wait here until a signal occurs--eg, kill from reader */


    return 0;
}

示例执行

./writeP &
> 24858   // memory address

./readP 24858

0 个答案:

没有答案