IPC in Unix of POSIX Standard

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

1.

)Write UNIX system program or programs to demonstrate the IPC mechanism


shared memory-oriented system calls
#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 /* make it a 1K shared memory segment */
int main(int argc, char *argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if (argc > 2) {
fprintf(stderr, "usage: shmdemo [data_to_write]\n");
exit(1);
}
/* make the key: */
if ((key = ftok("2.c", 'R')) == -1) {
perror("ftok");
exit(1);
}
printf( "key is %d ", key);
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
printf( "shmid is %d ", shmid);
/* attach to the segment to get a pointer to it: */
data = shmat(shmid, (void *)0, 0);
if (data == (char *)(-1)) {
perror("shmat");
exit(1);
}
/* read or modify the segment, based on the command line: */
if (argc == 2) {
printf("writing to segment: \"%s\"\n", argv[1]);
strncpy(data, argv[1], SHM_SIZE);
} else
printf("segment contains: \"%s\"\n", data);
/* detach from the segment: */
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
return 0;
}
o/p:
sarat@ubuntu:~$ cc 2.c
sarat@ubuntu:~$ ./a.out
key is 1375736568 shmid is 2228246 segment contains: ""
sarat@ubuntu:~$ ipcs
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x520012f8 2228246 sarat 644 1024 0

2.)Write UNIX system program or programs to demonstrate the IPC mechanism


Message queues-oriented system calls.
//mq-sender
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_TEXT 512
struct my_msg_st {
long int my_msg_type;
char some_text[MAX_TEXT];
};
int main()
{
int running = 1;
struct my_msg_st some_data;
int msgid;
char buffer[BUFSIZ];
msgid = msgget((key_t)224, 0666 | IPC_CREAT);
if (msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}
while(running) {
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
some_data.my_msg_type = 1;
strcpy(some_data.some_text, buffer);
if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1) {
fprintf(stderr, "msgsnd failed\n");
exit(EXIT_FAILURE);
}
if (strncmp(buffer, "end", 3) == 0) {
running = 0;
}
}
exit(EXIT_SUCCESS);
}

o/p:
sarat@ubuntu:~$ cc 3.1.c
sarat@ubuntu:~$ ./a.out
Enter some text: sarat
Enter some text: y8cs224
Enter some text: sujit
Enter some text: end
sarat@ubuntu:~$ ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
0x000000e0 0 sarat 666 3072 6

//receiver for above program


/*mq-receiver*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msg_st {
long int my_msg_type;
char some_text[BUFSIZ];
};
int main()
{
int running = 1;
int msgid;
struct my_msg_st some_data;
long int msg_to_receive = 0;
//msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
/*if (msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}*/
while(running) {
if (msgrcv(0, (void *)&some_data, BUFSIZ,
msg_to_receive, 0) == -1) {
fprintf(stderr, "msgrcv failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}
printf("You wrote: %s", some_data.some_text);
if (strncmp(some_data.some_text, "end", 3) == 0) {
running = 0;
}
}
/* if (msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}*/
exit(EXIT_SUCCESS);
}

o/p:
sarat@ubuntu:~$ cc 3.2.c
sarat@ubuntu:~$ ./a.out
You wrote: sarat
You wrote: y8cs224
You wrote: sujit
You wrote: end

3.)Write UNIX system program or programs to demonstrate the IPC mechanism


using semaphores.

/* semint.c */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
union semun {
int val; /* used for SETVAL only */
struct semid_ds *buf; /* for IPC_STAT and IPC_SET */
ushort *array; /* used for GETALL and SETALL */
};
int main(void)
{
key_t key;
int semid;
union semun arg1;
if ((key = ftok("semdemo.c", 'J')) == -1) {
perror("ftok");
exit(1);
}
/* create a semaphore set with 1 semaphore: */
if ((semid = semget(key, 1, 0666 | IPC_CREAT)) == -1) {
perror("semget");
exit(1);
}
/* initialize semaphore #0 to 1: */
arg1.val = 1;
if (semctl(semid, 0, SETVAL, arg1) == -1) {
perror("semctl");
exit(1);
}
return 0;
}

/* semdemo.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 = {0, -1, 0}; /* set to allocate resource */
if ((key = ftok("semdemo.c", 'J')) == -1) {
perror("ftok");
exit(1);
}
/* grab the semaphore set created by seminit.c: */
if ((semid = semget(key, 1, 0)) == -1) {
perror("semget");
exit(1);
}
printf("Press return to lock: ");
getchar();
printf("Trying to lock...\n");
if (semop(semid, &sb, 1) == -1) {
perror("semop");
exit(1);
}
printf("Locked.\n");
printf("Press return to unlock: ");
getchar();
sb.sem_op = 1; /* free resource */
if (semop(semid, &sb, 1) == -1) {
perror("semop");
exit(1);
}
printf("Unlocked\n");
return 0;
}

/* semrm.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;
union semun arg;
if ((key = ftok("semdemo.c", 'J')) == -1) {
perror("ftok");
exit(1);
}
/* grab the semaphore set created by seminit.c: */
if ((semid = semget(key, 1, 0)) == -1) {
perror("semget");
exit(1);
}
/* remove it: */
if (semctl(semid, 0, IPC_RMID, arg) == -1) {
perror("semctl");
exit(1);
}
return 0;
}

You might also like