简体   繁体   中英

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.

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. You can then read/write based on the return value of 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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