0% found this document useful (0 votes)
35 views6 pages

Lab 6 Exemplul 1:citeste de La TST Si Le Transmite Prin Coada de Mesaje

The document describes examples of using inter-process communication (IPC) mechanisms on Linux including message queues, semaphores and shared memory. Example 1 creates a message queue and sends messages to it. Example 2 receives messages from the queue created in Example 1. Example init.c creates a semaphore set with an initial value of 1. Example semafor.c uses the semaphore to lock a shared resource. Example memorie.c demonstrates reading from and writing to shared memory, while delete.c removes the shared memory segment.

Uploaded by

Miculescu Kathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Lab 6 Exemplul 1:citeste de La TST Si Le Transmite Prin Coada de Mesaje

The document describes examples of using inter-process communication (IPC) mechanisms on Linux including message queues, semaphores and shared memory. Example 1 creates a message queue and sends messages to it. Example 2 receives messages from the queue created in Example 1. Example init.c creates a semaphore set with an initial value of 1. Example semafor.c uses the semaphore to lock a shared resource. Example memorie.c demonstrates reading from and writing to shared memory, while delete.c removes the shared memory segment.

Uploaded by

Miculescu Kathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 6

Exemplul 1 :citeste de la tst si le transmite prin coada de mesaje

/* [ex1.c] */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf {
long mtype;
char mtext[200];
};
int main(void){
struct msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("ex1.c", 'c')) == -1) {//creare cheie
perror("Eroare creare cheie");
exit(1);
}
if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
perror("Eroare creare coada de mesaje");
exit(1);
}
printf("Introduceti text, apasati CTRL-D pentru a iesi:\n");
buf.mtype = 1;
while(gets(buf.mtext), !feof(stdin)) {
if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf), 0)
== -1)
perror("Eroare send");
}
//se sterge coada de mesaje
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("Eroare stergere coada");
exit(1);
}
return 0;
}
Exemplul 2: citeste mesaje din coada de mesaje create in exemplul 1.

/* [ex2.c] */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf {
long mtype;
char mtext[200];
};
int main(void){
struct msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("ex1.c", 'c')) == -1) { //aceeasi cheie ca in
ex1.c
perror("Eroare creare cheie");
exit(1);
}
if ((msqid = msgget(key, 0644)) == -1) { //conectare la coada de //mesaje
perror("Eroare la conectare ");
exit(1);
}
printf("Proces %d primesc mesaje\n", getpid()

for(;;) {
//bucla infinita, se termina la eroare la receptie
if (msgrcv(msqid, (struct msgbuf *)&buf, sizeof(buf),0,0) == -1){
perror("Eroare receptie");
exit(1);
}
printf("Proces %d: \"%s\"\n", getpid(), buf.mtext);
}
return 0;
}

LAB 7- SEMAFOARE
Prog init.c creeaza un set de semafoare cu un semafor,cu val initiala 1.se va testa cu
com ipcs

/* [initc.c] */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

Int main(void) {
Key_t key ;
Int semid, semval ;
If((key= ftok("semafor.c",’a’))==-1) {
Perror("eroare creare cheie");
Exit(1);
}
/*creare set de semafoare cu un semafor*/
Semval=1;
If(semctl(semid,0,SETVAL,semval)==-1) {
Perror("eroare initializare semafor");
Exit(1);
}
Return 0;
}

/* [delete.c] */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
Int main(void) {
Key_t key ;
Int semid,semval ;
If((key=ftok("semafor.c",’a’)==-1) {
Exit(1);
}
/*conectare la semafor*/
If((semid=semget (key,1,0))==-1) {
Perror("eroare stergere");
Exit(1);
}
Return 0;
}
/* [semafor.c] */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

Int main(void) {
Key_t key ;
Int semid ;
Struct sembuf sb ;
Sb.sem_num=0 ;
Sb.sem_op=-1 ;/* pt ocuparea resursei */
Sb.sem_flg=0 ;
If((key=ftok("semafor.c",’a’))==-1){
Perror("eroare creare cheie");
Exit(1);
}
Printf("apasa enter pt a ocupa resursa:");
Getchar();
Printf("incerc sa ocup resursa…\n");

If(semop(semid, &sb,1)==-1) {
Perror("eroare semop");
Exit(1);
}
Printf("resursa este ocupata.\n");
Printf("apasati enter pt eliberarea resursei: “);
Getchar();

Sb.sem_op=1;
If(semop(semid, &sb,1)==-1){
Perror(“eroare semop”);
Exit(1);
}
Printf(“resursa este eliberata.\n”);
Return 0;
}

LAB 8 –MODALITATI DE IPC

In urm. Ex se afiseaza datele din mem partajata,iar daca se da al doilea


argument,procesul scrie in mem partajata datele din argv[1].
Pt stergerea mem partajate se foloseste programul delete.c

/* [memorie.c] */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024 /*marimea memoriei 1024 octeti */
Int main(int argc,char *argv[])
{
Key_t key ;
Int shmid ;
Char * adresa ;
If(argc>2) {
Fprintf(stderr, “Utilizare:memorie[date_de_scris_in_memorie]\n“);
Exit(1);
}
/*se creeaza cheia */
If((key=ftok(“memorie.c“,’a’))==-1) {
Perror(“eroare creare cheie“);
Exit(1):
}
/*conectare la mem si creare daca este cazul */
If((shmid=shmget(key,SHM_SIZE,0644 | IPC_CREATE))=-1) {
Perror(“eroare creare/conectare“);
Exit(1);
}
/*adresarea in spatial de adrese al procesului */
Adresa=shmat(shmid,(void *)0,0);
If(adresa==(char*)(-1)) {
Perror(“eroare conectare“);
Exit(1);
}
/* se modifica zona de memorie,se citeste continutul ei */
If(argc==2) {
Printf(“scriu in zona de memorie:\ “%s\“\n“,argv[1]);
Strncpy(adresa,argv[1],SHM_SIZE);
}else
Printf(“continutul memoriei:\ “&s\“\n“,adresa);
/*deconectare*/
If(shmdt(adresa)==1) {
Perror(“eroare deconectare“);
Exit(1);
}
Return 0;
}

/* [delete.c] sterge zona de memorie partajata */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024 /* marimea memoriei 1024 octeti */

Int main(void)
{
Key_t key ;
Int shmid ;
Char *adresa ;
/* se creeaza cheia,aceeasi ca la memorie.c */
If((key=ftok(“memorie.c“,’a’))=-1) {
Perror(“eroare creare cheie“);
Exit(1);
}
/*conectare la memorie */
If((shmid=shmget(key, SHM_SIZE, 0644))==-1) {
Perror (“eroare creare/conectare“);
Exit(1);
}
If ((shmctl(sgmid, IPC_RMID,0))<0) {
Perror(“eroare stergere zona de memorie cu id=%d“,shmid);
Exit(1)
}
Return 0;
}

You might also like