SO2 kolokwium1 programy sci

LAB2

1. ===========

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main ()

{ pid_t potomek;

potomek = fork ();

if (potomek != 0) {

printf ("RODZIC: ID procesu biezacego: %d\n", getpid ());

printf ("ID dziecka: %d\n", potomek);}

else {printf ("DZIECKO: ID procesu biezacego: %d\n", getpid ());

printf ("ID rodzica: %d\n", getppid ());}return 0;}

2.==============

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

int main ()

{pid_t potomek;

//Tworzenie procesu potomnego

potomek = fork ();

if (potomek > 0) {

//To jest proces rodzica, ktory spi przez 60 sec

sleep (10);}else {

//To jest proces dziecka, ktory konczy sie natychmiast

exit (0);}return 0;}

3. ==============

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

#include <stdlib.h>

int main ()

{ int status;

int potomek;

potomek = fork ();

//proces macierzysty

if (potomek != 0) {

wait(&status);

if (WIFEXITED(status) > 0) {

printf("status: %d\n", WEXITSTATUS(status));}

else printf("Cos jest nie tak");}

//proces potomny

else {printf("proces potomny...\n");

exit(0);} return 0;}

4. =============

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

#include <stdlib.h>

void dziecko(int a);

int main (int argc, char *argv[]){

int status;

int pid;

pid_t potomek;

int i, j;

for(i = 1; i < atoi(argv[1])+1; i++) {

potomek = fork ();

if (potomek == 0) {

pid = getpid();

for(j = 0; j < 4; j++)

printf("Potomek: %d PID: %d PPID: %d\n", i, getpid(), getppid());

sleep(i);

exit(0);}}

while ((pid = wait(&status)) > 0)

printf("\nPID: %d, status: %d\n", pid, status);

return 0;}

void dziecko(int a){ }

5.=====================

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/wait.h>

int main (){

int status;

int potomek;

potomek = fork ();

if (potomek == 0) {

execlp("./zad5a", "zad5a", 0);}

wait(&status);return 0;}

----------------------------

#include <stdio.h>

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

printf("\nAlbert lubi lowic ryby\n\n");

return 0;}

6. ======================

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <signal.h>

void obsluz(int sygnal)

{printf("------------------------\n");

printf("Otrzymano sygnal nr: %d\n", sygnal);

printf("------------------------\n");}

int main(int argc, char **argv)

{signal(SIGALRM, obsluz);

int pid = getpid();

kill(pid, SIGALRM);

printf("Koniec programu.\n");

return 0;}

7..==============

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <signal.h>

#include <sys/wait.h>

void obsluz(int sygnal)

{printf("------------------------\n");

printf("Otrzymano sygnal nr: %d\n", sygnal);

printf("------------------------\n");}

int main(int argc, char **argv)

{int potomek;

int status;

signal(SIGINT, obsluz);

potomek = fork();

if (potomek == 0) {

kill(getppid(), SIGINT);}

else {wait(&status);

printf("Koniec programu.\n");}

return 0;}

8. ==================

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <signal.h>

#include <sys/wait.h>

void obsluz(int sygnal)

{printf("Otrzymano sygnal nr: %d\n", sygnal);}

int main(int argc, char **argv)

{ int p1, p2, p3, p4;

int a = 4, b = 4, c = 4, d = 4;

int status;

p1 = fork();

p2 = fork();

if(p1 == 0) {

signal(SIGINT, obsluz);

while(1) {

printf("proces 1...\n");

pause();

sleep(1);

kill(p2, SIGALRM);a--;}}

if(p2 == 0) {

signal(SIGALRM, obsluz);

while(1) {

printf("proces 2...\n");

pause();

sleep(1);

kill(p1, SIGINT);b--;}}

wait(&status);

return 0; }

9. ====================

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

#include <stdlib.h>

int main(int argc, char **argv)

{ int potomek;

int status;

int dana = 5;

potomek = fork();

if (potomek == 0) {

printf("Potomek: %d\n", dana);

printf("Potomek: %d\n", dana += 5);}

else {wait(&status);

printf("Rodzic: %d\n", dana);

printf("Rodzic: %d\n", dana += 5); return 0; }

10. =======================

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <signal.h>

#include <sys/wait.h>

void obsluz(int sygnal)

{printf("------------------------\n");

printf("Funkcja obslugujaca sygnal SIGINT\n");

printf("Otrzymano sygnal nr: %d czyli SIGINT\n", sygnal);

printf("------------------------\n");}

void skomplikowana_funkcja()

{printf("UWAGA !!! Skomplikowana operacja.\n");}

int main(int argc, char **argv)

{int potomek;

int status;

signal(SIGINT, obsluz);

potomek = fork();

if (potomek == 0) {

kill(getppid(), SIGINT);}

else {wait(&status);

skomplikowana_funkcja();

printf("Koniec programu.\n");}return 0;}

11. ======================

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <signal.h>

#include <sys/wait.h>

int main(int argc, char **argv)

{int potomek;

int status;

sigblock(SIGINT);

potomek = fork();

if (potomek == 0) {

kill(getppid(), SIGINT);}

else {wait(&status);

printf("Koniec programu.\n");}return 0;}

12. =================

LAB3==================

1. =================

#include <stdio.h>

#include <stdlib.h>

main()

{FILE *fp;

char tekst[20] = "";

fp = popen("sort zad1.c","r");

fread(tekst,1,20,fp);

printf("%s\n",tekst);

pclose(fp);}

2===============

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

main(){

FILE *fp;

char tekst[256];

fp = popen("wc","w");

gets(tekst);

fwrite(tekst,1,strlen(tekst),fp);

pclose(fp);

}

3============

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

main()

{int pipefd[2];

char msg[256];

char msg2[256];

gets(msg);

if(pipe(pipefd) == -1)

{printf("error");}

write(pipefd[1],msg,strlen(msg));

read(pipefd[0],msg2,strlen(msg));

printf("%s\n",msg2);

close(pipefd[0]);

close(pipefd[1]);}

4=========================

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <fcntl.h>

main()

{int pipefd[2], cpid;

char buf, msg[256];

printf("%d\n",pipe2(pipefd,O_NONBLOCK));

cpid = fork();

if(cpid == -1){

perror("fork");

exit(EXIT_FAILURE);}

if (cpid == 0 )

{close(pipefd[1]);

while(read(pipefd[0],&buf,1) > 0)

{write(STDOUT_FILENO,&buf,1);}

write(STDOUT_FILENO,"\n",1);

close(pipefd[0]);

exit(EXIT_SUCCESS);}

Else {

close(pipefd[0]);

gets(msg);

write(pipefd[1],msg,strlen(msg));

close(pipefd[1]);

wait();

exit(EXIT_SUCCESS);}}

5. ===========================

#include <stdio.h>

#include <stdlib.h>

main()

{int i = 1, j, cpid, pipefd[2];

pipe(pipefd);

cpid = fork();

if(cpid == 0)

{read(pipefd[0],&j,sizeof(j));

printf("%d\n",j);

i = 2;

write(pipefd[1],&i,sizeof(i));

close(pipefd[0]);

close(pipefd[1]);

exit(1);}

else{write(pipefd[1],&i,sizeof(i));

wait();

read(pipefd[0],&j,sizeof(j));

printf("%d\n",j); }}

6. ===================

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "myfifo"

int main()

{int fd, num;

char msg[256];

mkfifo(FIFO_NAME,0666);

perror("mkfifo");

printf("Czekanie na odbiorce...\n");

fd = open(FIFO_NAME, O_WRONLY);

printf("Znaleziono odbiorce\n");

while(!feof(stdin))

{gets(msg);

if((num = write(fd,msg,strlen(msg))) == -1)

{perror("write");}

else{printf("Nadawca wyslal: %d bajtow\n",num);}}}

--------

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "myfifo"

int main(void)

{char s[300];

int num, fd;

mkfifo(FIFO_NAME,0666);

printf("Oczekiwanie na nadawce\n");

fd = open(FIFO_NAME, O_RDONLY);

printf("Jest nadawca\n");

do {if ((num = read(fd, s, 256)) == -1)

perror("read");

else {s[num] = '\0';

printf("Odbiorca przeczytal: %d bajtow: \"%s\"\n", num, s);}

} while (num > 0);return 0; }

7. ===============

#include <stdio.h>

#include <stdlib.h>

#include <error.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <fcntl.h>

#define FIFO_NAME "myFIFO2"

main()

{int pfds[2],num,fd,fd1;

char buf[30];

if(pipe(pfds) == -1)

{perror("pipe");

exit(1);}

if(mkfifo(FIFO_NAME,0666) == -1)

{perror("mkfifo");}

if(!fork())

{close(pfds[1]);

if((num = read(pfds[0],buf,30)) == -1)

{perror("read");}

else

{buf[num] = '\0';

printf("P2: Odebralem : %s, %d bajtow\n",buf,num);

printf("P2: Podaj napis do wyslania: ");

gets(buf);

fd = open(FIFO_NAME,O_WRONLY);

write(fd,buf,strlen(buf));

exit(0);}}

else{printf("P1: Podaj napis do wyslania: ");

gets(buf);

close(pfds[0]);

write(pfds[1],buf,strlen(buf));

fd = open(FIFO_NAME,O_RDONLY);

num = read(fd,buf,30);

buf[num] = '\0';

printf("P1: Odebralem: %s, %d bajtow\n",buf,num);

unlink(FIFO_NAME);

wait();}}

8=================

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <unistd.h>

#define FIFO_NAME "myFIFO"

main()

{int fd,num;

char msg[256];

if(mknod(FIFO_NAME, S_IFIFO | 0666,0) == -1)

{perror("mknod");}

while(!feof(stdin))

{fd = open(FIFO_NAME, O_WRONLY);

printf("Podaj wiadomosc: ");

gets(msg);

write(fd,msg,strlen(msg));

close(fd);

fd = open(FIFO_NAME, O_RDONLY);

printf("Wyslano.\nOczekiwanie na odpowiedz...\n");

num = read(fd,msg,256);

msg[num] = '\0';

printf("%s\n",msg);

close(fd);}

unlink(FIFO_NAME);}

-----

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "myFIFO"

main(){

int fd,num;

char msg[256];

while(!feof(stdin))

{fd = open(FIFO_NAME,O_RDONLY);

printf("Oczekiwanie na odpowiedz...\n");

num = read(fd,msg,256);

msg[num] = '\0';

printf("%s\n",msg);

close(fd);

fd = open(FIFO_NAME,O_WRONLY);

printf("Podaj wiadomosc: ");

gets(msg);

write(fd,msg,strlen(msg));

printf("Wyslano. ");

close(fd);} }

9. ===================

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

main()

{int pfds[2],p0[4],i;

pipe(pfds);

if(fork() == 0)

{if(fork() == 0)

{if(fork() == 0)

{int p3[4];

read(pfds[0],p3,sizeof(p3));

for(i = 0;i<4;i++)

printf("%d\n",p3[i]);

exit(3);}

int p2[4];

read(pfds[0],p2,sizeof(p2));

for(i = 0;i<4;i++)

p2[i] += 1;

write(pfds[1],p2,sizeof(p2));

wait();

exit(2);}

int p1[4];

read(pfds[0],p1,sizeof(p1));

for(i = 0;i<4;i++)

p1[i] += 1;

write(pfds[1],p1,sizeof(p1));

wait();

exit(1);}

else{for(i = 0;i<4;i++)

{p0[i] = i+1;}

close(pfds[0]);

write(pfds[1],p0,sizeof(p0));

wait();}}

10. ===================

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "myFIFOa"

main()

{int fd,i = 1;

if(mkfifo(FIFO_NAME,0666) == -1)

{perror("mkfifo");}

fd = open(FIFO_NAME,O_WRONLY);

while(1)

//for(i = 1;i<=9;i+=2)

{sleep(2);

if(write(fd,&i,sizeof(i)) > 0)

{printf("P1: %i\n",i);

i+=2;}}close(fd);}

------

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "myFIFOb"

main({

int fd,i=2;

if(mkfifo(FIFO_NAME,0666) == -1)

perror("mkfifo");

fd = open(FIFO_NAME,O_WRONLY);

while(1)

//for(i = 2;i<=10;i+=2)

{ sleep(2);

if(write(fd,&i,sizeof(i)) > 0)

{printf("P2: %i\n",i);

i+=2;}}

close(fd);

unlink(FIFO_NAME);}

------

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME1 "myFIFOa"

#define FIFO_NAME2 "myFIFOb"

main(){

int fd1,fd2,a,b;

fd1 = open(FIFO_NAME1,O_RDONLY);

fd2 = open(FIFO_NAME2,O_RDONLY);

while(1)

{if((read(fd1,&a,sizeof(a)) <= 0) && (read(fd2,&b,sizeof(b)) <= 0))

exit(1);

printf("SUMA: %d\n",a+b);}

close(fd1);

close(fd2);}

LAB4=============

1. ===============.

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf; //zmienna przez ktora przekazywane sa parametry operacji

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

char *mtext; //tresc komunikatu };

struct msgbuf bufor1, bufor2;

int main()

{ int id_msgget; //identyfikator kolejki

char *wiadomosc = "message ..."; //tresc komunikat

id_msgget = msgget(IPC_PRIVATE, IPC_CREAT|0600);

printf("Id msgget %d\n", id_msgget);

bufor1.mtype = 1; //typ komunikatu

bufor1.mtext = wiadomosc; //tresc komunikatu

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor1.mtype); //rozmiar komunikatu

int wysylanie = msgsnd (id_msgget, &bufor1, rozmiar, 0); //wyslanie komunikatu

if (wysylanie == -1)

perror("Blad wyslania");

msgrcv(id_msgget, &bufor2, rozmiar, 1, 0); //odebranie komunikatu

printf("Odebrany komunikat: %s\n", bufor2.mtext);

msgctl(id_msgget, IPC_RMID, buf); //usuniecie kolejki

return 0; }

2. =================

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/wait.h>

struct msqid_ds *buf; //zmienna przez ktora przekazywane sa parametry operacji

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

char *mtext; //tresc komunikatu };

struct msgbuf bufor1, bufor2;

int main()

{ int id_msgget; //identyfikator kolejki

char *wiadomosc = "message ..."; //tresc komunikat

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor1.mtype); //rozmiar komunikatu

pid_t pid;

id_msgget = msgget(IPC_PRIVATE, IPC_CREAT|0600);

if ((pid = fork()) == 0) { //potomek

printf("Id msgget %d\n", id_msgget);

bufor1.mtype = 1; //typ komunikatu

bufor1.mtext = wiadomosc; //tresc komunikatu

int wysylanie = msgsnd (id_msgget, &bufor1, rozmiar, 0); //wyslanie komunikatu

if (wysylanie == -1)

perror("Blad wyslania");

exit(0); }

else { //rodzic

wait(0);

msgrcv(id_msgget, &bufor2, rozmiar, 1, 0); //odebranie komunikatu

printf("Odebrany komunikat: %s\n", bufor2.mtext);

msgctl(id_msgget, IPC_RMID, buf);//usuniecie kolejki

return 0;}}

3. =============

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok; //identyfikator kolejki

id_ftok = ftok("zad3a.c",120);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

printf("Id msgget %d\n", id_msgget);

bufor.mtype = 1; //typ komunikatu

bufor.mnumber = 5; //tresc komunikatu

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

int wyslanie = msgsnd (id_msgget, &bufor, rozmiar, 0); //wyslanie komunikatu

if (wyslanie == -1)

perror("Blad wyslania");

printf("Wyslano komunikat: %d\n", bufor.mnumber);

return 0;}

-------

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf; //zmienna przez ktora przekazywane sa parametry operacji

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok; //identyfikator kolejki

id_ftok = ftok("zad3a.c",120);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

msgrcv(id_msgget, &bufor, rozmiar, 1, 0); //odebranie komunikatu

printf("Odebrany komunikat: %d\n", bufor.mnumber);

msgctl(id_msgget, IPC_RMID, buf); //usuniecie kolejki

return 0; }

4. ====================

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok; //identyfikator kolejki

int i, wyslanie;

id_ftok = ftok("zad3a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

printf("Id msgget %d\n", id_msgget);

bufor.mtype = 1; //typ komunikatu

bufor.mnumber = 5; //tresc komunikatu

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

for(i = 0; i < 10000; i++) {

wyslanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT); //wyslanie komunikatu

++bufor.mnumber;

}

if (wyslanie == -1)

perror("Blad wyslania");

printf("Wyslano komunikat: %d\n", bufor.mnumber);return 0;}

--------

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf; //zmienna przez ktora przekazywane sa parametry operacji

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu

};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok; //identyfikator kolejki

int i;

id_ftok = ftok("zad3a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

for(i = 0; i < 10000; i++) {

msgrcv(id_msgget, &bufor, rozmiar, 1, 0); //odebranie komunikatu

printf("Odebrany komunikat: %d\n", bufor.mnumber);

}

msgctl(id_msgget, IPC_RMID, buf); //usuniecie kolejki

return 0;}

5===================

#include <stdlib.h>

#include <time.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu};

struct msgbuf bufor;

int main()

{srand(time(NULL));

int id_msgget, id_ftok; //identyfikator kolejki

int i, wyslanie;

id_ftok = ftok("zad3a.c",120);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

printf("Id msgget %d\n", id_msgget);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

for(i = 1; i < 8; i++) {

bufor.mtype = 1+random()%5;

bufor.mnumber = bufor.mtype;

wyslanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT); //wyslanie komunikatu

if (wyslanie == -1)

perror("Blad wyslania");

printf("Wyslano komunikat: %d\n", bufor.mnumber);}return 0;}

-----

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf; //zmienna przez ktora przekazywane sa parametry operacji

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok; //identyfikator kolejki

int i, sprawdz;

id_ftok = ftok("zad3a.c",120);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

for(i = 1; i < 6; i++) {

do {

sprawdz = msgrcv(id_msgget, &bufor, rozmiar, i, IPC_NOWAIT); //odebranie komunikatu

if (sprawdz != -1)

printf("Odebrany komunikat: %d\n", bufor.mnumber);

} while (sprawdz != -1);}

msgctl(id_msgget, IPC_RMID, buf); //usuniecie kolejki

return 0;}

6. ===================

#include <stdlib.h>

#include <time.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu};

struct msgbuf bufor;

int main()

{srand(time(NULL));

int id_msgget, id_ftok; //identyfikator kolejki

int i, wyslanie;

id_ftok = ftok("zad3a.c",120);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

printf("Id msgget %d\n", id_msgget);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

for(i = 1; i < 11; i++) {

bufor.mtype = i;

bufor.mnumber = bufor.mtype;

wyslanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT); //wyslanie komunikatu

if (wyslanie == -1)

perror("Blad wyslania");

printf("Wyslano komunikat: %d\n", bufor.mnumber);}return 0;}

--------

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

#include<stdlib.h>

struct msqid_ds *buf; //zmienna przez ktora przekazywane sa parametry operacji

struct msgbuf { //struktura komunikatu

long mtype; //typ komunikatu

int mnumber; //tresc komunikatu };

struct msgbuf bufor;

int main(int argc, char **argv)

{ if (argc != 2) {

printf("Prosze podac jeden parametr !!!\n");

exit(EXIT_FAILURE); }

int id_msgget, id_ftok; //identyfikator kolejki

int i;

id_ftok = ftok("zad3a.c",120);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype); //rozmiar komunikatu

if (atoi(argv[1]) == 0) {

for(i = 1; i < 11; i++) {

msgrcv(id_msgget, &bufor, rozmiar, i, IPC_NOWAIT); //odebranie komunikatu

printf("Odebrany komunikat: %d\n", bufor.mnumber);} }

else if (atoi(argv[1]) == 1) {

for(i = 10; i > 0; i--) {

msgrcv(id_msgget, &bufor, rozmiar, i, IPC_NOWAIT); //odebranie komunikatu

printf("Odebrany komunikat: %d\n", bufor.mnumber);} }

msgctl(id_msgget, IPC_RMID, buf); //usuniecie kolejki

return 0; }

7. ==================

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf;

struct msgbuf {

long mtype;

int mnumber;};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok, i, odbierz;

id_ftok = ftok("zad7a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);

for(i = 2; i <= 3; i++)

{bufor.mtype = i;

bufor.mnumber = 1;

int wysylanie = msgsnd (id_msgget, &bufor, rozmiar, 0);

printf("Wyslano do procesu %d\n", i);

if (wysylanie == -1)

perror("Blad wyslania");}

do{odbierz = msgrcv(id_msgget, &bufor, rozmiar, 1, IPC_NOWAIT);

if (odbierz != -1) printf("Wiadomosc od procesu: %d\n", bufor.mnumber);

}while(odbierz != -1);

do{

printf("Usunac kolejke? [1]-TAK [2]-NIE ");

scanf("%d", &i);

} while ((i != 1) && (i != 2));

if (i == 1)

msgctl(id_msgget, IPC_RMID, buf);return 0;}

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf;

struct msgbuf {

long mtype;

int mnumber;};

struct msgbuf bufor;

int main()

{int id_msgget, id_ftok, i, odbierz;

id_ftok = ftok("zad7a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);

for(i = 1; i <= 3; i += 2)

{bufor.mtype = i;

bufor.mnumber = 2;

int wysylanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT);

printf("Wyslano do procesu %d\n", i);

if (wysylanie == -1)

perror("Blad wyslania: ");}

do{odbierz = msgrcv(id_msgget, &bufor, rozmiar, 2, IPC_NOWAIT);

if (odbierz != -1) printf("Wiadomosc od procesu: %d\n", bufor.mnumber);

} while (odbierz != -1);

do{printf("Usunac kolejke? [1]-TAK [2]-NIE ");

scanf("%d", &i);

} while ((i != 1) && (i != 2));

if (i == 1)

msgctl(id_msgget, IPC_RMID, buf);return 0;}

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf;

struct msgbuf {

long mtype;

int mnumber;};

struct msgbuf bufor;

int main(){

int id_msgget, id_ftok, i, odbierz;

id_ftok = ftok("zad7a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);

for(i = 1; i <= 2; i++){

bufor.mtype = i;

bufor.mnumber = 3;

int wysylanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT);

printf("Wyslano do procesu %d\n", i);

if (wysylanie == -1)

perror("Blad wyslania: ");}

do{odbierz = msgrcv(id_msgget, &bufor, rozmiar, 3, IPC_NOWAIT);

if (odbierz != -1) printf("Wiadomosc od procesu: %d\n", bufor.mnumber);

} while (odbierz != -1);

do{printf("Usunac kolejke? [1]-TAK [2]-NIE ");

scanf("%d", &i);

} while ((i != 1) && (i != 2));

if(i == 1)

msgctl(id_msgget, IPC_RMID, buf);return 0;}

8. ======================

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

#include <string.h>

struct msgbuf {

long mtype;

char mtext[50];};

struct msgbuf2 {

long mtype;

char mtext[100];};

struct msgbuf3 {

long mtype;

int mrozmiar;};

struct msgbuf bufor;

struct msgbuf2 bufor2;

struct msgbuf3 bufor3;

int main(){

int id_ftok, id_msgget, rozmiar = 0, wyslanie;

id_ftok = ftok("zad8a.c", 127);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

/* komunikat 1 */

bufor.mtype = 1;

strcpy(bufor.mtext, "Stawialismy zagle");

//rozmiar = sizeof(struct msgbuf);

/* rozmiar komunikatu 1 */

bufor3.mtype = 3;

bufor3.mrozmiar = sizeof(bufor.mtext);

rozmiar = sizeof(struct msgbuf3);

wyslanie = msgsnd (id_msgget, &bufor3, rozmiar, 0);

if (wyslanie == -1)

perror("Blad przeslania:");

printf("Wyslano wiadomosc: \"rozmiar: %d\"\n", bufor3.mrozmiar);

/* dodanie komunikatu 1 do kolejki */

rozmiar = sizeof(struct msgbuf);

wyslanie = msgsnd (id_msgget, &bufor, rozmiar, 0);

if (wyslanie == -1)

perror("Blad przeslania:");

printf("Wyslano: \"%s\"\n", bufor.mtext);

/* komunikat 2 */

bufor2.mtype = 2;

strcpy(bufor2.mtext, "Za burte poszly sieci");

/* rozmiar komunikatu 2 */

//rozmiar = sizeof(struct msgbuf2);

bufor3.mrozmiar = sizeof(bufor2.mtext);

rozmiar = sizeof(struct msgbuf3);

wyslanie = msgsnd (id_msgget, &bufor3, rozmiar, 0);

if (wyslanie == -1)

perror("Blad przeslania:");

printf("Wyslano wiadomosc: \"rozmiar: %d\"\n", bufor3.mrozmiar);

/* dodanie komunikatu 2 do kolejki */

rozmiar = sizeof(struct msgbuf2);

wyslanie = msgsnd (id_msgget, &bufor2, rozmiar, 0);

if (wyslanie == -1)

perror("Blad przeslania:");

printf("Wyslano: \"%s\"\n", bufor2.mtext);return 0;}

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf;

struct msgbuf {

long mtype;

char mtext[50];};

struct msgbuf2 {

long mtype;

char mtext[100];};

struct msgbuf3 {

long mtype;

int mrozmiar;};

struct msgbuf bufor;

struct msgbuf2 bufor2;

struct msgbuf3 bufor3;

int main()

{int id_ftok,id_msgget, rozmiar, i;

id_ftok = ftok("zad8a.c", 127);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

for(i = 0; i < 2; i++) {

rozmiar = sizeof(struct msgbuf3);

msgrcv(id_msgget, &bufor3, rozmiar, 3, 0);

printf("Rozmiar: %d\n", bufor3.mrozmiar);

if (bufor3.mrozmiar < 70) {

rozmiar = sizeof(struct msgbuf);

msgrcv(id_msgget, &bufor, rozmiar, 1, 0);

printf("Odebrana wiadomosc (1): %s\n", bufor.mtext);}

else {rozmiar = sizeof(struct msgbuf2);

msgrcv(id_msgget, &bufor2, rozmiar, 2, 0);

printf("Odebrana wiadomosc (2): %s\n", bufor2.mtext);}}

msgctl(id_msgget, IPC_RMID, buf);return 0;}

9. =======================

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf {

long mtype;

int mnumber;};

struct msgbuf bufor;

int main(){

int id_msgget,id_ftok,i;

id_ftok = ftok("Zad9a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);

for (i = 1; i <= 10; i += 2) {

bufor.mtype = 1;

bufor.mnumber = i;

int wyslanie = msgsnd (id_msgget, &bufor, rozmiar, 0);

printf("Przeslano: %d\n", bufor.mnumber);

if (wyslanie == -1)

perror("Blad przesylania:");}return 0;}

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf {

long mtype;

int mnumber;};

struct msgbuf bufor;

int main(){

int id_msgget, id_ftok, i;

id_ftok = ftok("Zad9a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);

for (i = 2; i <= 10; i += 2) {

bufor.mtype = 2;

bufor.mnumber = i;

int wyslanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT);

printf("Przeslano: %d\n", bufor.mnumber);

if (wyslanie == -1)

perror("Blad przesylania:");}return 0;}

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msqid_ds *buf;

struct msgbuf {

long mtype;

int mnumber;};

struct msgbuf bufor, bufor2;

int main()

{int id_msgget, id_ftok, i;

id_ftok = ftok("Zad9a.c", 269);

id_msgget = msgget(id_ftok, IPC_CREAT|0660);

int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);

for (i = 1; i <= 5; i++) {

msgrcv(id_msgget, &bufor,rozmiar, 1, IPC_NOWAIT);

msgrcv(id_msgget, &bufor2,rozmiar, 2, IPC_NOWAIT);

printf("%d + %d = %d\n", bufor.mnumber, bufor2.mnumber, bufor.mnumber + bufor2.mnumber);}

msgctl(id_msgget, IPC_RMID, buf);return 0;}


Wyszukiwarka

Podobne podstrony:
SO2 kolokwium1 tresci sci
SO2 kolokwium1 sci
Przykładowe zadania na 2 kolokwium z programowania w języku C, Studia, PWR, 1 semestr, Podstawy prog
Przykładowe zadania na 1 kolokwium z programowania w języku C, Studia, PWR, 1 semestr, Podstawy prog
zadania na kolokwium-programowanie, Automatyka i robotyka air pwr, II SEMESTR, Programowanie w język
Kolokwium1 - Nowak(wyklad), Studia WIT - Informatyka, Programowanie C
program i zagadnienia do kolokwium, Technologia chemiczna, 5 semestr, odpady
program i kolokwium jagiellonsk Nieznany
zestawy-labC++-kolokwium 2 2006-2007, Politechnika Śląska MT MiBM, Semestr III, Języki programowania
c-kolokwium1, wisisz, wydzial informatyki, studia zaoczne inzynierskie, podstawy programowania, kol
kolokwium-zestawy-pytań-i-odpowiedzi-2, Politechnika Śląska MT MiBM, Semestr III, Języki programowan
kolokwium2-zap, opis programow, Wszystkie pliki poza zad1
Wilkosz, Wstęp do programowania, kolokwia KD1-09 10l
Wilkosz, Wstęp do programowania, kolokwia K2-08 09l
Wilkosz, Wstęp do programowania, kolokwia K2-10 11l
programy z kolokwium

więcej podobnych podstron