interprocess communication--two processes operating the same file

Source: Internet
Author: User

The contents of the A.txt file are as follows: Hello,world. Write two different executable programs with names A and B, respectively. The open function is called in a program and the A.txt file is opened, and the B program cannot call open or fopen. Only allow the read function to be called to implement reading the A.txt file (a program can use the fork and EXECV functions to create a child process).
Makefile
. Suffixes:. C. occ=gccsrcs=a.c    objs=$ (SRCS:.C=.O) Exec=aall: $ (OBJS)    $ (CC)-O $ (EXEC) $ (OBJS)     @echo '------ -------OK--------------'. C.O:    $ (CC)-wall-g-o [email protected]-C $< Clean:    rm-f $ (OBJS)    rm-f core*

A.c
 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <    Unistd.h>int Main () {printf ("a begin\n");    int fd = open ("A.txt", o_rdonly);        if (fd = =-1)//Open file failed {printf ("error is%s\n", Strerror (errno));    return-1; } pid_t pid = fork ();//fork call, clone a copy of yourself in memory if (PID < 0) {printf ("Fork failed%s\n", Strerror (errno        ));    return-1;        } if (PID > 0)//parent process {Close (FD);//parent process close Open file descriptor FD} if (pid = = 0)//Sub-process {char s[128];        memset (s, 0, sizeof (s));        sprintf (S, "%d", FD);//file descriptor after opening a.txt file FD formatted as String char *args[] = {"B", S, NULL}; if (Execve ("B", args, NULL) = =-1)//Pass the file descriptor FD as a startup parameter to the B program {printf ("Execve failed%s\n", Strerror (errno))        ; }} printf ("a end..    \ n "); return 0;} 

B.c
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/ types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>int main (int arg, char *args[] {printf ("B begin.) \ n "); if (args[1] = = NULL) When//b is started, if there are no arguments, the main function exits {printf (" usage:b xxx\n ") directly; return-1;} int fd = atoi (args[1]);//Convert the first argument from a string to an integer, this parameter is the file descriptor fdif (fd = = 0)//If the parameter is 0, the function returns {return-1;} Char Buf[1024];memset (buf, 0, sizeof (BUF)), read (FD, buf, sizeof (BUF));//Read the file printf ("%s", buf); close (FD);// Close the file descriptor printf ("B end.. \ n "); return 0;}



interprocess communication--two processes operating the same file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.