How to communication between processes?
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 int count=0;
5 void ctrl_c_count(int);
6 main()
7 {
8 int c;
9 void (* old_handler)(int);
10 old_handler=signal(SIGINT,ctrl_c_count);
11 while ((c=getchar())!='\n');
12 printf("Ctrl_C count=%d\n",count);
13 signal(SIGINT,old_handler);
14 }
15 void ctrl_c_count(int dump)
16 {
17 printf("Ctrl_C\n");
18 count++;
19 }
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 int main() {
6 pid_t pid; int fds[2]; char buf1[50],buf2[50];
7 pipe(fds); /* Create a pipe */
8 if ((pid=fork())==0) { /* Child process */
9 close(fds[0]);
10 sprintf(buf1,"data....\n");
11 write(fds[1],buf1,50);
12 exit(1);
13 } else { /* Parent process*/
14 close(fds[1]);
15 wait(0);
16 read(fds[0],buf2,50);
17 printf("I am parent, the message is %s\n",buf2);
18 }
19 }
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <limits.h>
7 init main() {
8 int fd,len; char buf[PIPE_BUF]; mode_t mode=0666;
9 if(mkfifo("fifo1",mode)<0) {
10 printf("mkfifo() err\n"); exit(1);
11 }
12 if((fd=open("fifo1",O_RDONLY))<0) {
13 printf("pipe open err\n"); exit(1);
14 }
15 while(len=(read(fd,buf,PIPE_BUF-1))>0) {
16 printf("%s\n",buf);
17 }
18 close(fd);
19 }
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <limits.h>
7 int main() {
8 int fd,len,i; mode_t mode=0666;
9 char buf[PIPE_BUF],buf1[PIPE_BUF];
10 if ((fd=open("fifo1",O_WRONLY))<0) {
11 printf("Pipe open err\n"); exit(1);
12 }
13 while(1) {
14 scanf("%s",buf1);
15 len=sprintf(buf,buf1);
16 write(fd,buf,len+1);
17 }
18 close(fd);
19 }
SysV IPC
POSIX IPC
Sockets
See example code. (Producer-Consumer in SysV IPC)
- Supported by the compiler. (Java, C#) - See example code. (Producer-Consumer in Java)
public class ProducerConsumer{
static final int N = 10;
static int current_item = 0;
static producer p = new producer();
static consumer c = new consumer();
static our_monitor mon = new our_monitor();
public static void main (String args[]){
p.start();
c.start();
}
static class producer extends Thread{
public void run(){
int item;
while(true){
item = produce_item();
mon.insert(item);
}
}
private int produce_item(){
current_item++;
waste_time();
return current_item;
}
private static void waste_time(){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class consumer extends Thread{
public void run(){
int item;
while(true){
item = mon.remove();
consume_item(item);
}
}
private void consume_item(int item){
waste_time();
}
private static void waste_time(){
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class our_monitor{
private int buffer[] = new int[N];
private int count = 0,lo = 0, hi = 0;
public synchronized void insert(int val){
if(count == N) {
System.out.println("Buffer full, producer sleeping...");
go_to_sleep();
}
buffer[hi] = val;
hi = (hi+1)%N;
count = count + 1;
System.out.println("Item " + current_item + " is produced.");
System.out.print("BUFFER: ");
for(int i=0;i<N;i++){
System.out.print(buffer[i]+" ");
}
System.out.println();
if (count == 1) {
System.out.println("Waking up the consumer...");
notify();
}
}
public synchronized int remove() {
int val;
if (count == 0) {
System.out.println("Buffer empty, consumer sleeping...");
go_to_sleep();
}
val = buffer[lo];
buffer[lo] = 0;
lo = (lo+1)%N;
count = count -1;
System.out.println("Item " + val + " is consumed.");
System.out.print("BUFFER: ");
for(int i=0;i<N;i++){
System.out.print(buffer[i]+" ");
}
System.out.println();
if (count == N-1) {
System.out.println("Waking up the producer...");
notify();
}
return val;
}
private void go_to_sleep(){
try{
wait();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
(a) Processes approaching a barrier.
(b) All processes but one blocked at the barrier.
(c) When the last process arrives at the barrier, all of them are let through.
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |