简体   繁体   English

子进程写入文件

[英]child process write to file

i have to make program, which will make two children processes. 我必须编写程序,这将使两个子进程。 These processes will write something(string...) in file. 这些过程将在文件中写入一些内容(字符串...)。 Parent process should decide which process is going to write to file i have created children processes, but i am stuck at these signals and i don't have a clue how to do this 父进程应该决定哪个进程将要写入文件,而我创建了子进程,但是我被这些信号所困扰,我不知道如何执行此操作

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#define READY_SIGNAL SIGUSR1
#define max 1000
int main(int argc, char *argv[]) {
        FILE *file;
        int o;
        char *name;
    opterr = 0;
        while ((o = getopt(argc, argv, "hp:")) != -1)
                switch (o) {
                        case 'h':
                        Help();
                        exit(1);

                        default:
                exit(1);
        }
    argc -= optind;
    argv += optind;
        if(argc==0){
        printf("file name\n");
        scanf("%s",&name);
        file=fopen(name,"a+");
        if( file != NULL)
        {
                printf("file created\n");
               // fclose(file);
         }

        else printf("the file does not exist\n");

        }
        else if(argc>1) {
                return(1);
        }
        else
                meno=argv[0];
        file=fopen(name,"a");
        if( file != NULL){
        printf("file created\n");
        }
        else printf("the file does not exist\n");

pid_t child_pid, child_pid2;

 printf ("the main program process ID is %d\n", (int) getpid());

 child_pid = fork () ;
 if (child_pid != 0) {
    printf ("this is the parent process, with id %d\n", (int) getpid ());
    printf ("the child's process ID is %d\n",(int) child_pid );
}
 else {
   printf ("this is the child process, with id %d\n", (int) getpid ());
exit(0);
}

 child_pid2 = fork () ;
 if (child_pid2 != 0) {
    printf ("this is the parent process, with id %d\n", (int) getpid ());
    printf ("the child's process ID is %d\n",(int) child_pid2 );
}
 else
{
   printf ("this is the child process, with id %d\n", (int) getpid ());
   exit(0);
}
 return 0;

}

thanks 谢谢

To start with your child processes are exiting as soon as they are created. 首先,子进程将在创建后立即退出。 If they didn't then the first child would then create a child of it's own. 如果没有,则第一个孩子将创建一个自己的孩子。 You probably want to create the children in a for loop and do something like: 您可能想在for循环中创建子代,然后执行以下操作:

if(child_pid[i] != 0)
{
    /* This is the parent. */
}
else
{
    /* This is the child. */
    do_child_stuff();
    exit(0);
}

It is a bad idea to open the file before you fork(). 在fork()之前打开文件是个坏主意。 You will end up with three processes who all hold a file handle to the same file with the same permissions. 您将以三个进程结束,这三个进程都拥有具有相同权限的同一文件的文件句柄。 Life starts getting complicated if you do that! 如果那样做,生活就会变得复杂! In general only open files when you really need to, and close them as soon as you have finished using them. 通常,仅在确实需要时打开文件,并在使用完毕后立即将其关闭。


I think what your question means is that you want the parent process to tell the child to write by sending a signal from the parent to the child. 我认为您的问题意味着您希望父进程通过从父进程向子进程发送信号来告诉子进程进行写操作。 There are easier ways of doing this, but I guess your teacher wants you to demonstrate how to do it with signals. 这样做有更简单的方法,但是我想您的老师希望您演示如何通过信号来做到这一点。

First off, you need to write a signal handler. 首先,您需要编写一个信号处理程序。 See http://linux.die.net/man/2/signal for more information on how to do this. 有关如何执行此操作的更多信息,请参见http://linux.die.net/man/2/signal

Second you need to actually send the signal. 其次,您需要实际发送信号。 See http://linux.die.net/man/2/kill for more information. 有关更多信息,请参见http://linux.die.net/man/2/kill Note that the name "kill" is a bit of a misnomer. 请注意,名称“ kill”有点用词不当。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM