CS631 - Advanced Programming in The UNIX Environment Interprocess Communication I
CS631 - Advanced Programming in The UNIX Environment Interprocess Communication I
System V IPC
All three use IPC structures, referred to by an identifier and a key; all
three are (necessarily) limited to communication between processes on
one and the same host.
Since these structures are not known by name, special system calls
(msgget(2), semop(2), shmat(2), etc.) and special userland commands
(ipcrm(1), ipcs(1), etc.) are necessary.
$ cc -Wall semdemo.c
1$ ./a.out
2$ ./a.out
$ ipcs -s
$ ipcrm -s 1234
1$ ./a.out
^Z
2$ ./a.out
$ cc -Wall shmdemo.c
$ ./a.out "Cow says: ’Moo!’"
$ ./a.out
$ ipcs -m
$ cc -Wall memory-layout.c
$ ./a.out
array[] from 804a080 to 8053cc0
stack around bffff9e4
malloced from 8053cc8 to 806c368
shared memory attached from 40162000 to 4017a6a0
$ ./mqrecv wait
Pipes: pipe(2)
#include <unistd.h>
int pipe(int filedes[2]);
Returns: 0 if OK, -1 otherwise
Pipes: pipe(2)
Pipes: pipe(2)
Pipes: pipe(2)
Pipes: pipe(2)
$ cc -Wall pipe1.c
$ ./a.out
P=> Parent process with pid 23988 (and its ppid 7474).
P=> Sending a message to the child process (pid 23989):
C=> Child process with pid 23989 (and its ppid 23988).
C=> Reading a message from the parent (pid 23988):
Hello child! I’m your parent pid 23988!
$
Pipes: pipe(2)
$ cc -Wall pipe1.c
$ ./a.out
P=> Parent process with pid 23984 (and its ppid 7474).
P=> Sending a message to the child process (pid 23985):
C=> Child process with pid 23985 (and its ppid 1).
C=> Reading a message from the parent (pid 1):
Hello child! I’m your parent pid 23984!
$
Pipes: pipe(2)
$ cc -Wall pipe1.c
$ ./a.out
P=> Parent process with pid 23986 (and its ppid 7474).
P=> Sending a message to the child process (pid 23987):
C=> Child process with pid 23987 (and its ppid 23986).
C=> Reading a message from the parent (pid 1):
Hello child! I’m your parent pid 23986!
$
Pipes: pipe(2)
$ ./a.out pipe2.c
[...]
^Z
$ ps -o pid,ppid,comm
PID PPID COMMAND
22306 26650 ./a.out pipe2.c
22307 22306 more
23198 26650 ps -o pid,ppid,comm
26650 26641 -ksh
$ fg
$ env PAGER=/bin/cat ./a.out pipe2.c
Pipes: pipe(2)
#include <unistd.h>
int pipe(int filedes[2]);
Returns: 0 if OK, -1 otherwise
$ cc -Wall popen.c
$ echo $PAGER
$ ./a.out popen.c
[...]
$ env PAGER=/bin/cat ./a.out popen.c
[...]
$
$ cc -Wall popen.c
$ echo $PAGER
$ ./a.out popen.c
[...]
$ env PAGER=/bin/cat ./a.out popen.c
[...]
$ env PAGER=/bin/cat/foo ./a.out popen.c
sh: /bin/cat/foo: Not a directory
$ env PAGER="more; touch /tmp/boo" ./a.out popen.c
$ env PAGER="more; rm /etc/passwd 2>/dev/null" ./a.out popen.c
FIFOs: mkfifo(2)
#include <sys/stat.h>
int mkfifo(const char *path, mode t mode);
Returns: 0 if OK, -1 otherwise
FIFOs: mkfifo(2)
FIFOs: mkfifo(2)
$ mkfifo fifo
$ grep pattern fifo > match &
$ gzcat file.gz | tee fifo | grep -v pattern > nomatch
https://stevens.netmeister.org/631/f18-hw2.html
Reading
https://stevens.netmeister.org/631/ipctut.pdf
https://stevens.netmeister.org/ipc.pdf
https://beej.us/guide/bgipc/html/single/bgipc.html
https://is.gd/M2dkju