创建子进程和父进程

时间:2014-03-02 13:14:11

标签: c linux sockets process parent-child

int main()
{
    pid_t cpid, w;
    int status;

    CreateSocket();   // it recievs data from the client

    cpid = fork();
    if (cpid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (cpid == 0) {            /* Code executed by child */
        CHECKTASKS();            //timer created for calling the task for every 2ms,10
    } else {
        do {
            w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
            if (w == -1) {
                perror("waitpid");
                exit(EXIT_FAILURE);
            }

            if (WIFEXITED(status)) {
                printf("exited, status=%d\n", WEXITSTATUS(status));
            } else if (WIFSIGNALED(status)) {
                printf("killed by signal %d\n", WTERMSIG(status));
            } else if (WIFSTOPPED(status)) {
                printf("stopped by signal %d\n", WSTOPSIG(status));
            } else if (WIFCONTINUED(status)) {
                printf("continued\n");
            }
        } while (!WIFEXITED(status) && !WIFSIGNALED(status));
        exit(EXIT_SUCCESS);
    }
}

我创建了一个子进程,我正在调用一个CreateSocket(); //它通过IP地址和端口号从客户端收回数据。我在创建子进程之前调用它。如果我喜欢那样 - 它会是一个父进程吗?我在创建子进程后调用另一个函数,即CHECKTASKS(); //为每2ms,10ms和100ms调用任务而创建的计时器//在后台运行。 CHECKTASKS是否将作为单独的进程在后台运行。是否可以在c中为linux OS编写如上所述的代码?

0 个答案:

没有答案