Simple use of piping and signals

Source: Internet
Author: User

2015.3.3
Tuesday Cloudy

IPC: Inter-process communication
Pipe: Nameless Pipes: can only be used between processes that have affinity
FIFO: Well-known pipeline: can make each other two processes communicate with each other, the famous pipeline can be indicated by the path name, and visible in the file system, through the file IO operation, does not support Lseek ()

Pipeline creation:

Nameless: Call pipe ();
Famous pipe: 1.mkfifo (), 2.open

Signal Processing: There is a stage: "The eve of the Revolution"


Implement inter-process communication, using the system call pipe () to establish a pipeline, two sub-processes to each of the pipeline to write a sentence, the parent process from the pipeline read out, and display.
The following code has a lot of details not good, only for reference, basic functions can be achieved.

Alarm ();
Pause (), need to set a handler function for the signal, or the program will be killed directly

Method of signal Processing:
Signal (); Deploy a signal processing function
signal set function;

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

int Pid1,pid2;

int main (int argc,char **argv)
{
int fd[2];
Char outpipe[100], inpipe[100];

Pipe (FD);
while ((Pid1 = fork ()) = =-1);

if (Pid1 = = 0)
{
sprintf (Outpipe, "Child 1 process is sending message!");
Write (fd[1],outpipe,50);
Sleep (2);
Exit (0);
}

Else
{
while ((Pid2 = fork ()) = =-1);

if (Pid2 = = 0)
{
sprintf (Outpipe, "Child 2 process is sending message!");
Write (fd[1],outpipe,50);
Sleep (2);
Exit (0);
}
Else
{
Wait (NULL); Synchronous
Read (fd[0],inpipe,50);
printf ("%s\n", inpipe);
Sleep (1);
Wait (NULL);
Read (fd[0],inpipe,50);
printf ("%s\n", inpipe);
Wait (NULL);
Exit (0);
}
}
}


Program Run Effect:

[Email protected]:/mnt/hgfs/source test/file io$ gcc pipe51.c
[Email Protected]:/mnt/hgfs/source test/file io$./a.out
Child 2 process is sending message!
Child 1 process is sending message!
[Email Protected]:/mnt/hgfs/source test/file io$

Create, read, and write operations for famous pipelines:

Create:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#define BUFFER_SIZE 1024

int main (int argc,char **argv)
{
int FD;

if (ARGC < 2)
{
fprintf (stdout, "usage:%s<filename>\n", argv[0]);
Exit (-1);
}

if (Mkfifo (argv[1],0644) < 0)
{
fprintf (stderr, "Mkfifo () Filed:%s\n", Strerror (errno));
Exit (-1);
}

return 0;
}

Write operation:

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

#define BUFFER_SIZE 1024

int main (int argc,char **argv)
{
int FD;

if (ARGC < 2)
{
fprintf (stdout, "usage:%s<filename>\n", argv[0]);
Exit (-1);
}

if (fd = open (argv[1],o_wronly)) < 0)
{
fprintf (stderr, "open FIFO%s for writting faile:%s\n", Argv[1],strerror (errno));
Exit (-1);
}

fprintf (stdout, "open FIFO%s for writting successed.\n", argv[0]);
Char Buffer[buffer_size];
ssize_t N;

while (Fgets (Buffer,buffer_size,stdin))
{
if ((n = write (Fd,buffer,strlen (buffer))) < 0)
{
fprintf (stderr, "write () failed on FIFO:%s\n", Strerror (errno));
Break
}
}

return 0;
}


Read operation:

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

#define BUFFER_SIZE 1024

int main (int argc,char **argv)
{
int FD;

if (ARGC < 2)
{
fprintf (stdout, "usage:%s<filename>\n", argv[0]);
Exit (-1);
}

if (fd = open (argv[1],o_rdonly)) < 0)
{
fprintf (stderr, "open FIFO%s for reading faile:%s\n", Argv[1],strerror (errno));
Exit (-1);
}

fprintf (stdout, "open FIFO%s for reading successed.\n", argv[0]);
Char Buffer[buffer_size];
ssize_t N;

while (1)
{
if ((n = read (fd,buffer,buffer_size)) < 0)
{
fprintf (stderr, "read failed on%s:%s\n", Argv[1],strerror (errno));
Exit (-1);
}
else if (n = = 0)
{
fprintf (stderr, "peer closed fifo.\n");
Break
}
Else
{
buffer[n]= ' + ';
fprintf (stdout, "read%d bytes from:%s", N,buffer);
}
}

return 0;
}

Note: Three programs to implement the corresponding function, when executing, create three executable files, the first to execute the program created, and then execute read and write programs, here: Read and write at the same time open to read and write operations to achieve, otherwise it will block

Use of signal functions:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>

pid_t pid;

void Driver_handler (int signo);
void Saler_handler (int signo);

int main (int argc, char **argv)
{
if ((PID = fork ()) < 0)
{
Perror ("fork");
Exit (-1);
}
if (PID > 0)
{
Signal (Sigtstp,driver_handler);
Signal (sigint,sig_ign);
Signal (sigquit,sig_ign);
Signal (Sigusr1,driver_handler);
Signal (Sigusr2,driver_handler);
while (1)
{
Pause ();
}
}
Else
{
Signal (Sigint,saler_handler);
Signal (Sigquit,saler_handler);
Signal (sigtstp,sig_ign);
Signal (Sigusr1,saler_handler);
Signal (sigusr2,sig_ign);
while (1)
{
Pause ();
}
}

return 0;
}

void Driver_handler (int signo)
{
if (Signo = = SIGUSR1)
{
printf ("Let's gogogo!\n");
}

if (Signo = = SIGUSR2)
{
printf ("Stop the bus!\n");
}

if (Signo = = SIGTSTP)
{
Kill (PID,SIGUSR1);
}
}

void Saler_handler (int signo)
{
pid_t ppid = Getppid ();
if (Signo = = SIGINT)
{
Kill (PPID,SIGUSR1);
}

if (Signo = = sigquit)
{
Kill (PPID,SIGUSR2);
}

if (Signo = = SIGUSR1)
{
printf ("Please get off the bus\n");
Kill (Ppid,sigkill);
Exit (0);
}
}

*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************

Simple use of piping and signals

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.