Instruction

by Paweł Paduch published 2020/11/12 09:20:39 GMT+2, last modified 2020-11-12T09:21:59+02:00
Creation and communication using PIPE links

PIPE Links

The purpose of this lab is to learn the principle of creating unnamed links and the principles of communication between two related processes.

    1. Introduction
      • We can only use unnamed (PIPE) communication links between two related processes.
      • We create it using function:
        int pipe(int *filedes);
        • filedes - two-element array
        • filedes[0] - is the file descriptor open for reading
        • filedes[1] - is the file descriptor open for writing
      • We usually use communication links to communicate between two processes, but the example below will be on one:
        main()
        {
        int pipefd[2],n;
        char buff[100];
        if (pipe(pipefd) <0)
            perror("pipe error\n");
        printf("read fd = %d, write fd = %d\n",pipefd[0],pipefd[1]);
        if (write(pipefd[1],"hello world\n",12) !=12)
            perror("write error\n");
        if ((n = read(pipefd[0],buff,sizeof(buff))) <=0)
            perror("read error\n");
        write(1, buff, n); /* file desc=1 stdout */
        exit(0);
        }
    2. Aim
      Write a client-server program that:
      • gets the file name from the user 0,5 point
      • creates an unnamed link PIPE 0,5 point
      • creates a new process using the fork function(not popen!) 0,5 points
      • passes the file name to the child process. 0,5pkt
      • the child process reads the file with the given name and the content (text) or information about possible errors (e.g. no file) is sent to the parent process (0.5 point)
      • parent process displays on standard output what it received from the child (correct file transfer up to 2 points).
    3. It can be usefull:
      • gets or fgets
      • perror, strerror
      • read, write
      • fork, wait
      • pipe, close
      • strcat, strlen, strcpy

    4. Final remark
      One should remember about the memory allocation for transmitted messages. It is also important that the pipe link is used only for one-way data transfer. If we want to have two-way communication, we need to create 2 links. 1 close a read desc in child and a write desc in parent, and the other close a write desc in child and read esc in parent. The file can be of any length, it can be read and sent in a loop (not a single reading). Do not thoughtlessly use memset , do not use single-character reading, don't combine with sending special bytes to end the transmission. The read function is a synchronous function.

    5. Report
      Of course, don't forget the report.