Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <semaphore.h>
- #include <sys/shm.h>
- #include <sys/msg.h>
- #define QKEY 0x1111
- struct msq_que
- {
- long msg_type;
- char buffer[2];
- //buffer[size-1] - polecenie zakonczenia pracy
- //buffer[size-2] - ilosc zmiennych, ktore zostaly przeslane
- }msq;
- void koniec(int sign)
- {
- fprintf(stderr, "\nKoniec. Czyszczenie pamieci.\n");
- }
- int main(int argc, char *arg[]){
- char cho,c,d;
- int i;
- int pid=getpid();
- printf("Proces macierzysty, PID: %d\n",pid);
- int msqid;
- msqid=msgget(QKEY, 0666 | IPC_CREAT);
- msq.msg_type = 1;
- signal(SIGQUIT, koniec);
- if (fork()==0){
- FILE *file=fopen(arg[1],"r"); //plik wejściowy
- FILE *file2=fopen(arg[2],"r"); //plik wejściowy
- if (file == NULL)
- {
- perror("Nie udalo sie otworzyc pliku_1");
- }
- if (file2 == NULL)
- {
- perror("Nie udalo sie otworzyc pliku_2");
- }
- while ((c=fgetc(file)) !=EOF){
- d=fgetc(file2);
- msq.buffer[0]=c;
- msq.buffer[1]=d;
- //fprintf(stderr,"%c",msq.buffer[0]);
- msgsnd(msqid, &msq, sizeof(msq), 0);
- }
- c=fgetc(file);
- msq.buffer[0]=c;
- msgsnd(msqid, &msq, sizeof(msq), 0);
- fclose(file);
- fclose(file2);
- return 0;
- }
- if (fork()==0){
- FILE *output=fopen("test","w"); //plik wyjściowy
- msgrcv(msqid, &msq, sizeof(msq), 1, 0);
- while(msq.buffer[0]!=EOF){
- cho=msq.buffer[0];
- //fprintf(stderr,"%c",cho);
- fprintf(output,"%c",cho);
- cho=msq.buffer[1];
- //fprintf(stderr,"%c",cho);
- fprintf(output,"%c",cho);
- msgrcv(msqid, &msq, sizeof(msq), 1, 0);
- }
- fclose(output);
- msgctl(msqid, IPC_RMID, NULL); //usuniecie queue
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement