创建子进程,让子进程的父进程传输文件。我该怎么做?

时间:2014-10-12 19:55:36

标签: c linux fork

这是我的代码。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>


int file,parentID,childID;
pid_t pid;

int main(int argc, char *argv[])
{

    if( argc != 2 )
    {
        printf("ERROR ! You have not write an argument\n");
        printf("ERROR ! You give more than one argument");
        return 1;
    }

    file = open(argv[1], O_RDONLY);  //open file

    if(file<0)  //test the file
    {
        printf("Error open file\n");
        printf("ERROR : %s\n", strerror(errno));
        return 1;
    }

// ---------------------------------------------------------------------

    pid =  fork();


    if( pid == -1)      //error fork
    {
        printf("Error fork\n");
        return 1;
    }


    if(pid ==  0) // child process
         {
        childID = getpid();
        printf("Child process %d\n",childID);
    //      if(childID %2 == 1)
    //  {
    //      parentID = getppid();
    //      printf("Process of father of this child= %d\n",parentID);
    //  }
             }

    if( pid == 1)
    {
        parentID = getppid();
        printf("ParentProcess %d\n",parentID);
    }

}

我必须编写一个程序来创建子进程。根据子进程的奇偶校验,父进程应该通过文件向子进行消息传递,消息被子进程接管并显示(如果儿童过程是一个可以分解的数字2,它会说 - &#34;早上好!&#34;否则&#34;晚安!&#34;)。父母应该等待孩子的最终执行终止。

我真的很努力地做这个练习,我无法解释我应该如何或者用什么功能/结构对象来做这个。我试过但是我失败了,我了解叉子怎么做但是......请帮我解释一下这个代码,或者建议我应该去读一下这个练习。抱歉我的英文拼写错误。

2 个答案:

答案 0 :(得分:1)

您使用哪些文档进行系统调用?

有很多方法可以做到这一点,但您可能想要做的是创建一个管道,然后分叉该过程。由于fork复制了所有内容,并且子进程继承了环境,因此每个进程都有一个管道文件描述符的副本。然后,您可以根据fork()的返回值进行读/写。

int main(int argc, char *argv[])
{
    int fd[2];
    char in[128], out[128];

    if( argc != 2 )
    {
        printf("ERROR ! You have not write an argument\n");
        printf("ERROR ! You give more than one argument");
        return 1;
    }

    if (pipe(fd) == -1)
        return 1;

// ---------------------------------------------------------------------

    pid =  fork();

    if (!pid)
        read(fd[0], in, 128);
    else
        write(fd[1], out, strlen(out) + 1);

请注意,您通常希望关闭您不会用于单向通信的文件描述符

答案 1 :(得分:0)

我认为这是代码。

#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

void sighandler (int sig)
{
    if (sig == SIGUSR1)
    {
    FILE *f;
    char line[100];
    f = fopen("file","r");
    fgets(line, 100, f);
    printf ("Procesul copil cu pid %d a primit mesajul %s", getpid(), line);
    fclose(f);
    }
}

int main ()
{
    pid_t pid;
    FILE * f; 

  pid = fork();
  if (pid < 0)
    {
      perror ("Eroare la fork()");
      return (1);
    }
  else
  if (pid == 0)
  {

    signal (SIGUSR1, sighandler);

    pause();

    return 0;    
  }
  else
  {
    if (pid % 2 == 0)
    {
        printf ("Notificam procesul fiu cu pid %d", pid);
    f = fopen ("file","w");
        fprintf (f,"Good morning!");
    fclose(f);
    kill (pid, SIGUSR1);
    }
    else 
    {
    printf ("Notificam procesul fiu cu pid %d", pid);
    f = fopen ("file","w");
    fprintf (f,"Good night!");
    fclose(f);
    kill (pid, SIGUSR1);
    }
  }

  wait(NULL);
  return 0;
}