Professional Documents
Culture Documents
OS Imp Questions PDF
OS Imp Questions PDF
pipe.
The entire IPC process will consist of three programs:
Program1: to create a named pipe
Program2: process that will write into the pipe (sender process)
Program3: process that will receive data from pipe (receiver process)
Program1: Creating fifo/named pipe ( 1.c )
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int res;
res = mkfifo("fifo1",0777); //creates a named pipe with the name fifo1
printf("named pipe created\n");
}
\
<fcntl.h>
int main()
{
int res,n;
res=open("fifo1",O_WRONLY);
write(res,"Message",7);
printf("Sender Process %d sent the data\n",getpid());
}
int main()
{
key_t key = ftok("shmfile",65);
int shmid = shmget(key,1024,0666|IPC_CREAT);
char *str = (char*) shmat(shmid,(void*)0,0);
cout<<"Write Data : ";
gets(str);
printf("Data written in memory: %s\n",str);
shmdt(str);
return 0;
}
int main()
{
key_t key = ftok("shmfile",65);
int shmid = shmget(key,1024,0666|IPC_CREAT);
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Data read from memory: %s\n",str);
shmdt(str);
shmctl(shmid,IPC_RMID,NULL);
return 0;
}
3)Write a program to implement ipc using message queues.
MESSAGE QUEUE FOR WRITER PROCESS
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;
printf("Write Data : ");
fgets(message.mesg_text,MAX,stdin);
msgsnd(msgid, &message, sizeof(message), 0);
printf("Data send is : %s \n", message.mesg_text);
return 0;
}
MESSAGE QUEUE FOR READER PROCESS
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
msgrcv(msgid, &message, sizeof(message), 1, 0);
printf("Data Received is : %s \n", message.mesg_text);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
4)Write a program to implement ipc using unnamed pipe.
/* this example shows how unnamed pipes may be used */
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main() {
int ret_val;
int pfd[2];
char buff[32];
char string1[]="String for pipe I/O";
int main() {
FILE *fp;
char buffer[100];
void *fun1()
{
int x;
printf("Thread1 trying to acquire lock\n");
pthread_mutex_lock(&l); //thread one acquires the lock. Now thread 2 will not be able to
acquire the lock //until it is unlocked by thread 1
printf("Thread1 acquired lock\n");
x=shared;//thread one reads value of shared variable
printf("Thread1 reads the value of shared variable as %d\n",x);
x++; //thread one increments its value
printf("Local updation by Thread1: %d\n",x);
sleep(1); //thread one is preempted by thread 2
shared=x; //thread one updates the value of shared variable
printf("Value of shared variable updated by Thread1 is: %d\n",shared);
pthread_mutex_unlock(&l);
printf("Thread1 released the lock\n");
}
void *fun2()
{
int y;
printf("Thread2 trying to acquire lock\n");
pthread_mutex_lock(&l);
printf("Thread2 acquired lock\n");
y=shared;//thread two reads value of shared variable
printf("Thread2 reads the value as %d\n",y);
y--; //thread two increments its value
printf("Local updation by Thread2: %d\n",y);
sleep(1); //thread two is preempted by thread 1
shared=y; //thread one updates the value of shared variable
printf("Value of shared variable updated by Thread2 is: %d\n",shared);
pthread_mutex_unlock(&l);
printf("Thread2 released the lock\n");
}
void *fun1()
int x;
void *fun2()
{
int y;
y=shared;//thread two reads value of shared variable
printf("Thread2 reads the value as %d\n",y);
y--; //thread two increments its value
printf("Local updation by Thread2: %d\n",y);
sleep(1); //thread two is preempted by thread 1
shared=y; //thread one updates the value of shared variable
printf("Value of shared variable updated by Thread2 is: %d\n",shared);
}
void *fun1()
{
int x;
printf("Thread1 trying to acquire lock\n");
pthread_mutex_lock(&l); //thread one acquires the lock. Now thread 2 will not be able to
acquire the lock //until it is unlocked by thread 1
printf("Thread1 acquired lock\n");
x=shared;//thread one reads value of shared variable
printf("Thread1 reads the value of shared variable as %d\n",x);
x++; //thread one increments its value
printf("Local updation by Thread1: %d\n",x);
sleep(1); //thread one is preempted by thread 2
shared=x; //thread one updates the value of shared variable
printf("Value of shared variable updated by Thread1 is: %d\n",shared);
pthread_mutex_unlock(&l);
printf("Thread1 released the lock\n");
}
void *fun2()
{
int y;
printf("Thread2 trying to acquire lock\n");
pthread_mutex_lock(&l);
printf("Thread2 acquired lock\n");
y=shared;//thread two reads value of shared variable
printf("Thread2 reads the value as %d\n",y);
y--; //thread two increments its value
printf("Local updation by Thread2: %d\n",y);
sleep(1); //thread two is preempted by thread 1
shared=y; //thread one updates the value of shared variable
printf("Value of shared variable updated by Thread2 is: %d\n",shared);
pthread_mutex_unlock(&l);
printf("Thread2 released the lock\n");
}
int main() {
int fd;
char buf[10];
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
void *fun1()
{
int x;
sem_wait(&s); //executes wait operation on s
x=shared;//thread1 reads value of shared variable
printf("Thread1 reads the value as %d\n",x);
x++; //thread1 increments its value
printf("Local updation by Thread1: %d\n",x);
sleep(1); //thread1 is preempted by thread 2
shared=x; //thread one updates the value of shared variable
printf("Value of shared variable updated by Thread1 is: %d\n",shared);
sem_post(&s);
}
void *fun2()
{
int y;
sem_wait(&s);
y=shared;//thread2 reads value of shared variable
printf("Thread2 reads the value as %d\n",y);
y--; //thread2 increments its value
printf("Local updation by Thread2: %d\n",y);
sleep(1); //thread2 is preempted by thread 1
shared=y; //thread2 updates the value of shared variable
printf("Value of shared variable updated by Thread2 is: %d\n",shared);
sem_post(&s);
}
fp = fopen("opengenus.txt", "r");
if (fp == NULL) {
puts("cannot open this file");
exit(1);
}
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, (length - number), SEEK_SET);
do {
ch = fgetc(fp);
putchar(ch);
} while (ch != EOF);
fclose(fp);
return(0);
}
int main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\n Error ");
exit(1);
}
else if(pid==0)
{
printf("\n Hello I am the child process ");
printf("\n My pid is %d ",getpid());
exit(0);
}
else
{
printf("\n Hello I am the parent process ");
printf("\n My actual pid is %d \n ",getpid());
exit(1);
}
16)WAP to create a file with some lines of content and write “hello”
into that file after 4 characters from starting using system call.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char buffer[100] = "This is some content\n";
char hello[6] = "hello";
// Create a file
fd = open("test.txt", O_CREAT | O_WRONLY, 0644);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main() {
pthread_t thread1, thread2;
int numbers[2] = {5, 7};
// create thread 1
pthread_create(&thread1, NULL, printMessage, NULL);
// create thread 2
pthread_create(&thread2, NULL, addNumbers, (void*) numbers);
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main() {
pthread_t thread1, thread2, thread3;
int numbers[2] = {5, 7};
// create thread 1
pthread_create(&thread1, NULL, printMessage, NULL);
// create thread 2
pthread_create(&thread2, NULL, addNumbers, (void*) numbers);
// create thread 3
pthread_create(&thread3, NULL, printEndMessage, NULL);
return 0;
}
19)Write a program to create a child process. The parent
process prints 20-29 and child process prints 0-8 Also both
the process prints a common message
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pid = fork();
if (pid == -1) {
printf("Failed to create child process\n");
return 1;
}
int main() {
pthread_t thread;
return 0;
}
21)WAP to create the following hierarchy and verify the relationship using
getpid() and getppid().P1->P2->P3
22)WAP to create two threads. One will print the numbers from 1 to 10 and
the other thread will find whether a number entered by the user is even or
odd
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_exit(NULL);
}
if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
pthread_exit(NULL);
}
int main() {
pthread_t tid1, tid2;
int ret1, ret2;
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
23)Write a program to create following hierarchy and verify the
relationship between the processes. Where process P1 has 2
children P2 and P3.
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid1, pid2;
pid1 = fork();
if (pid1 == -1) {
printf("Failed to create P2\n");
return 1;
} else if (pid1 == 0) {
printf("I am P2 with pid %d and my parent is P1 with pid %d\n", getpid(), getppid());
return 0;
}
pid2 = fork();
if (pid2 == -1) {
printf("Failed to create P3\n");
return 1;
} else if (pid2 == 0) {
printf("I am P3 with pid %d and my parent is P1 with pid %d\n", getpid(), getppid());
return 0;
}
return 0;
}
a Create two files, write contents into them and merge them into
one
c. Change the permission of the file to user read, write and execute,
group read and execute, others read