0% found this document useful (0 votes)
7 views12 pages

IPC Mechanisms in Linux Programming

Uploaded by

phoneseller821
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)
7 views12 pages

IPC Mechanisms in Linux Programming

Uploaded by

phoneseller821
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

Operating System Lab Programs

Table of Content:

Lab Lab Title


No.:

1.​ Install RHEL/KALI/LINUX server with GUI using VMware

2.​ Program to create multiple threaded processes.

3.​ Implement IPC mechanism using shared memory & message passing

4.​ Program to simulate producer consumer problem using Semaphore.

5.​ Program to simulate and find Average TAT & Waiting Time for Preemptive & Non
Preemptive Scheduling Algorithms: FCFS, SJF, Priority & Round-Robin.

6.​ Program to simulate Contagious Memory Allocation technique: Worst Fit, Best Fit, &
First Fit.

7.​ Program to simulate Pare Replacement Algorithms: FIFO, LRU, & LFU"

8.​ Program to simulate Disk Scheduling Algorithms: FCFS, SCAN, & C-SCAN
LAB 3: IMPLEMENT IPC MECHANISM USING SHARED MEMORY & MESSAGE
PASSING.

Objectives:
To implement shared memory for passing data between programs in Linux using C.
To implement message passing to read and write data to the message queue without being
connected to each other in Linux using c.

Background Theory:
Intern-process communication (IPC) is a mechanism that allows processes to communicate with
each other and synchronize their actions. The communication between these processes can be
seen as a method of co-operation between them. Processes can communicate with each other
through. In this IPC model, a shared memory region is established which is used by the processes
for data communication This memory region is present in the address space of the process which
creates the shared memory segment. The processes who want to communicate with this process
should attach this memory segment into their address space.
In this model, the processes communicate with each other by exchanging messages. For this
purpose, a communication link must exist between the processes and it must facilitate at least
two operations send(message) and receive (message). The size of the message may be virtual or
fixed.

PPID:
In addition to a unique process ID, each process is assigned a parent process ID (PPID) that tells
which process started it. The PPID of the PIC of the process's parent.
For example, if process1 with a PID of 101 starts a process named process2, then process2 will
be given a unique PID, such as 3240, but it will be given the PPID of 101. It is a parent-child
relationship. A single parent process may spawn several child processes, each with a unique PID
but all sharing the same PPID.

MESSAGE SEND

Source Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define PERMS 0644

struct my_msgbuf {
long mtype;
char mtext[200];
};

int main(void) {
struct my_msgbuf buf;
int msqid;
int len;
key_t key;

system("touch [Link]");

if ((key = ftok("[Link]", 'B')) == -1) {


perror("ftok");
exit(1);
}

if ((msqid = msgget(key, PERMS | IPC_CREAT)) == -1) {


perror("msgget");
exit(1);
}
printf("Message queue: ready to send messages.\n");
printf("Enter lines of text, CTRL+D to quit:\n");

[Link] = 1; // message type

while (fgets([Link], sizeof [Link], stdin) != NULL) {


len = strlen([Link]);
if ([Link][len - 1] == '\n')
[Link][len - 1] = '\0'; // remove newline

if (msgsnd(msqid, &buf, len + 1, 0) == -1)


perror("msgsnd");
}
strcpy([Link], "end");
len = strlen([Link]);
if (msgsnd(msqid, &buf, len + 1, 0) == -1)
perror("msgsnd");

printf("Message queue: done sending messages.\n");


return 0;
}

MESSAGE RECEIVE:

Source Code:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define PERMS 0644

struct my_msgbuf {
long mtype;
char mtext[200];
};

int main(void) {
struct my_msgbuf buf;
int msqid;
int toend;
key_t key;

if ((key = ftok("[Link]", 'B')) == -1) {


perror("ftok");
exit(1);
}
if ((msqid = msgget(key, PERMS)) == -1) { // connect to the queue
perror("msgget");
exit(1);
}
printf("Message queue: ready to receive messages.\n");

for (;;) {
if (msgrcv(msqid, &buf, sizeof([Link]), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("Received: \"%s\"\n", [Link]);

toend = strcmp([Link], "end");


if (toend == 0)
break;
}

printf("Message queue: done receiving messages.\n");


system("rm [Link]");
return 0;
}

READ

Source Code:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#define BUF_SIZE 1024


#define SHM_KEY 0x1234

struct shmseg {
int cnt;
int complete;
char buf[BUF_SIZE];
};

int main() {
int shmid;
struct shmseg *shmp;

shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644 | IPC_CREAT);


if (shmid == -1) {
perror("Shared Memory");
return 1;
}

shmp = shmat(shmid, NULL, 0);


if (shmp == (void *) -1) {
perror("Shared Memory attach");
return 1;
}

while (shmp->complete != 1) {
printf("Segment contains: \"%s\"\n", shmp->buf);
if (shmp->cnt == -1) {
perror("read");
return 1;
}
printf("Reading Process: Shared Memory: Read %d bytes\n", shmp->cnt);
sleep(3);
}

printf("Reading Process: Reading Done, Detaching Shared Memory\n");


if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
}
printf("Reading Process: Complete\n");
return 0;
}

WRITE

Source Code:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#define BUF_SIZE 1024


#define SHM_KEY 0x1234

struct shmseg {
int cnt;
int complete;
char buf[BUF_SIZE];
};

int fill_buffer(char *bufptr, int size);

int main() {
int shmid, numtimes;
struct shmseg *shmp;
char *bufptr;
int spaceavailable;

shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644 | IPC_CREAT);


if (shmid == -1) {
perror("Shared memory");
return 1;
}
shmp = shmat(shmid, NULL, 0);
if (shmp == (void *) -1) {
perror("shared memory attach");
return 1;
}

bufptr = shmp->buf;
spaceavailable = BUF_SIZE;

for (numtimes = 0; numtimes < 5; numtimes++) {


shmp->cnt = fill_buffer(bufptr, spaceavailable);
shmp->complete = 0;
printf("Writing Process: Shared Memory Write: Wrote %d bytes\n", shmp->cnt);
sleep(3);
}

printf("Writing Process: Wrote %d times\n", numtimes);


shmp->complete = 1;

if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
}

if (shmctl(shmid, IPC_RMID, 0) == -1) {


perror("shmctl");
return 1;
}

printf("Writing Process: Complete\n");


return 0;
}

int fill_buffer(char *bufptr, int size) {


static char ch = 'A';
int filled_count;

memset(bufptr, ch, size - 1);


bufptr[size - 1] = '\0';
if (ch > 122)
ch = 65;

if (ch >= 65 && ch <= 122) {


if (ch >= 91 && ch <= 96) {
ch = 65;
}
}

filled_count = strlen(bufptr);
ch++;
return filled_count;
}

PID PPID

Source Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
int mypid, myppid;
printf("Program to know PID and PPID's information\n");
mypid = getpid();
myppid = getppid();
printf("My process ID is %d\n", mypid);
printf("My parent process ID is %d\n", myppid);
return 0;
}
Output:

MESSAGE SEND

Message queue: ready to send messages.


Enter lines of text, ^D to quit:
Hi, how are you?
Where do you live?
Hello, this is just a demo
Message queue: done sending messages.

MESSAGE RECEIVE:

Message queue: ready to receive messages.


Received: "Hi, how are you?"
Received: "Where do you live?"
Received: "Hello, this is just a demo"
Message queue: done receiving messages.

READ:

Segment contains: ""


Reading Process Shared Memory: Read 0 bytes
Segment contains: ""
Reading Process Shared Memory: Read 0 bytes
Segment contains: ""
Reading Process Shared Memory: Read 0 bytes
Segment contains: ""
Reading Process Shared Memory: Read 0 bytes
Segment contains: ""
Reading Process Shared Memory: Read 0 bytes

WRITE:
Writing Process: Shared Memory Write: Wrote 1023 bytes
Writing Process: Shared Memory Write: Wrote 1023 bytes
Writing Process: Shared Memory Write: Wrote 1023 bytes
Writing Process: Shared Memory Write: Wrote 1023 bytes
Writing Process: Shared Memory Write: Wrote 1023 bytes
Writing Process: Wrote 5 times
Writing Process: Complete

PID PPID

Program to know PID and PPID's information


My process IS is 10742My parent process IS is 4015

You might also like