Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- int main(int argc, char** argv)
- {
- int stevec=1;
- int n,N=2;
- int izh_cev = 0;
- int id = 0;
- char msg[100]; // sporocilo, ki ga bomo zapisali v cev
- int l = 0; // dolzina zapisanega sporocila
- mkfifo(msg, 0666);
- // odpremo oprimek do cevi za branje
- // metoda prekine izvajanje, dokler drug program ne odpre cev za branje
- izh_cev = open("izpis.txt",O_WRONLY);
- if(izh_cev < 0)
- {
- perror("tezava pri odpiranju cevi\n");
- return -1;
- }
- // zapisemo sporocilo v cev
- //l = write(izh_cev,msg,sizeof(msg));
- for(n = 0; n < N; n++){
- switch(fork()){
- case -1:
- perror("tezava pri klicu fork");
- return -1;
- case 0:
- //close(kom_cev[1]);
- //sprintf(pozdrav_str,"pozdrav! proces st. %d\n",j);
- //write(1,pozdrav_str,strlen(pozdrav_str));
- //read(kom_cev[0],buff,1); // proces tukaj caka, da stars zapre svoj oprimek za pisanje
- while(stevec<=10)
- {
- if(n==1 && stevec % 2 != 0)
- {
- sprintf(msg,"%d:prvi\n",getpid());
- write(izh_cev,msg,strlen(msg));
- }
- if(n==0 && stevec % 2 == 0)
- {
- sprintf(msg,"%d:drugi\n",getpid());
- write(izh_cev,msg,strlen(msg));
- }
- stevec++; usleep(10000);
- }
- _exit(0);
- }
- }
- if(l >= 0)
- {
- printf("pisanje v cev koncanon\n");
- }else
- {
- perror("tezave pri pisanju v cev\n");
- }
- // zapremo oprimek do cevi
- if(close(izh_cev)!=0)
- {
- perror("tezava pri zapiranju cevi\n");
- return -1;
- }
- for(n = 0; n < N; n++)
- wait();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement