简体   繁体   English

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

[英]Create child process, make parent of the child to transmit a file.How do i do this?

This is my code. 这是我的代码。

#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);
    }

}

I have to write a program to create a child process.Depending on the parity of the child process , the parent should transmit to child a message through a file , the message being taken over and showed by the child process( if the child process is a number that is divizible with 2 it will say -"Good morning!" else "Good night!" ).The parent should wait for the final execution of the child to terminate. 我必须编写一个程序来创建一个子进程。根据子进程的奇偶性,父进程应通过文件向子进程发送一条消息,该消息被子进程接管并显示(如果子进程是可以被2整除的数字表示:“早安!”否则“晚安!”)。父母应等待孩子的最终处决终止。

I'm trying really hard to do this exercise and i can't find anywere to explain me how or what function/structure object should i use to do this.Above i tried but i failed , and i understand somehow how fork does but... please help me with this code , or suggest me were should i go to read to make this exercise .Sorry for my bad english spelling. 我真的很努力地做这个练习,我找不到任何可以解释我应该如何或使用哪个功能/结构对象的方法。在上面我尝试了但失败了,但我知道叉子是怎么做的。 ..请帮助我使用此代码,或建议我去阅读该练习。对不起,我的英语拼写错误。

What documentation are you using for the system calls? 您正在使用什么文档进行系统调用?

There are a number of ways to do this, but what you probably want to do is create a pipe, and then fork the process. 有很多方法可以做到这一点,但是您可能想要做的是创建一个管道,然后派生该过程。 Since a fork copies everything, and child processes inherit the environment, each process has a copy of the file descriptors for the pipe. 由于fork复制了所有内容,并且子进程继承了环境,因此每个进程都有管道的文件描述符的副本。 You can then read/write based on the return value of 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);

note, you usually want to close the file descriptor you're not using for one way communication 请注意,您通常希望关闭不用于单向通信的文件描述符

I think this is the code. 我认为这是代码。

#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;
}

暂无
暂无

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

相关问题 如何让我的父母等待,直到中间父母的中间父母、孩子和孩子使用 fork、wait 和 waitpid 完成该过程? - How do I make my parent wait, until a Intermediate parent, child and child of Intermediate parent finish the process using fork, wait, and waitpid? 我如何在子进程内部等待直到父进程达到某个点? - How do I wait inside a child process until the parent process reached a certain point? 服务器中的父进程卡在接受呼叫中。 如何从孩子那里干净地终止父母 - Parent process in server stuck in accept call. How do I terminate the parent cleanly from child 父子进程是否并发执行 - do parent and child process execute concurrently 通过杀死其父级而成为孤儿的子进程未被init采用。 我该如何解决? - The child process which is made orphan by killing its parent is not getting adopted by init. How do I fix this? 如何让守护进程与 C 中的子进程通信 - How do I get daemon process to communicate with child process in C 创建了两个子进程。 父进程应该一直执行到一个子进程终止。 我如何在 c 中编写这个程序? - Two child process are created. The parent process should execute until one child process terminates. How do I write this program in c? 子进程如何告诉其父创建一个新子? - How can child process tell its parent to create a new child? fork()-让父进程执行工作而无需等待子进程 - fork() - have parent process do work without waiting for child process 每当父进程重新启动时如何使子进程死亡 - How to make child process die whenever parent process gets restarted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM