PROGRAM 3
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
sem_t chopstick[5];
void* philos(void*);
void eat(int);
int main() {
int i, n[5];
pthread_t T[5];
for(i = 0; i < 5; i++) {
sem_init(&chopstick[i], 0, 1);
}
for(i = 0; i < 5; i++) {
n[i] = i;
pthread_create(&T[i], NULL, philos, (void*)&n[i]);
}
for(i = 0; i < 5; i++) {
pthread_join(T[i], NULL);
}
return 0;
}
void* philos(void* n) {
int ph = *(int*)n;
printf("Philosopher %d wants to eat\n", ph);
printf("Philosopher %d tries to pick the left chopstick\n", ph);
sem_wait(&chopstick[ph]);
printf("Philosopher %d picks the left chopstick\n", ph);
printf("Philosopher %d tries to pick the right chopstick\n", ph);
sem_wait(&chopstick[(ph + 1) % 5]);
printf("Philosopher %d picks the right chopstick\n", ph);
eat(ph);
sem_post(&chopstick[(ph + 1) % 5]);
printf("Philosopher %d leaves the right chopstick\n", ph);
}
void eat(int ph)
{
printf("philospher % d begins to eat\n",ph);
}
PROGRAM 4
#include <stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i, j;
printf("Enter total number of processes (maximum 20): ");
scanf("%d", &n);
printf("Enter process Burst Time\n");
for (i = 0; i < n; i++) {
printf("p[%d]: ", i + 1);
scanf("%d", &bt[i]);
}
wt[0] = 0;
for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++) {
wt[i] += bt[j];
}
}
printf("\nProcess Burst Time | Waiting Time | Turnaround Time\n");
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
printf("p[%d]: \t%d \t\t%d \t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
avwt /= n;
avtat /= n;
printf("\nAverage Waiting Time: %d", avwt);
printf("\nAverage Turnaround Time: %d", avtat);
return 0;
}
PROGRAM 5
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void* shared_memory;
char buft[100];
int shmid;
shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);
printf("key of shared memory is %d\n", shmid);
shared_memory = shmat(shmid, NULL, 0);
printf("process attached at %p\n", shared_memory);
printf("Enter some data to write to shared memory: ");
read(0, buft, 100);
strcpy(shared_memory, buft);
printf("You wrote: %s\n", (char*)shared_memory);
return 0;
}
PROGRAM 6
for x in "$@"
do
if [ -f "$x" ];
then
echo "$x is s file"
echo "number of lines in a file are"
wc -l "$x"
elif [ -d "$x" ];
then
echo "$x is a directory"mkdi
else
echo "enter the valid filename ordirectory name"
fi
done
PROGRAM 7
#!/bin/bash
echo "enter the file name"
read file
awk '$o !~ /[aeiouAEIOU]/ { count++ }
END { print "the number of lines that doesnot contain vowels", count }' $file
PROGRAM 8
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char* argv[])
{
char d[50];
if(argc==2)
{
memset(d,0,sizeof(d));
strcat(d,"/s");
strcat(d,"_i");
strcat(d,argv[5]);
system(d);
}
else
{
printf("\n invalid number of inputs");
}
return 0;
}
PROGRAM 9
sender
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct message
{
long mtype;
char mtext[15];
};
int main()
{
int qid, len, i;
char s[15];
struct message message;
qid = msgget((key_t)10, IPC_CREAT | 0666);
if (qid == -1)
{
perror("message queue create failed");
exit(1);
}
for (i = 1; i <= 3; i++)
{
printf("Enter the message to send: ");
scanf("%s", s);
strcpy(message.mtext, s);
message.mtype = i;
len = strlen(message.mtext);
if (msgsnd(qid, &message, len + 1, 0) == -1)
{
perror("message send failed");
exit(1);
}
}
return 0;
}
receiver
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int qid, i;
struct {
long mtype;
char mtext[15];
} buff;
qid = msgget((key_t)10, IPC_CREAT | 0666);
if (qid == -1) {
perror("Message queue creation failed");
exit(1);
}
for (i = 1; i <= 3; i++) {
if (msgrcv(qid, &buff, sizeof(buff.mtext), i, 0) == -1) {
perror("Message receive failed");
exit(1);
}
printf("Message received from the sender: %s\n", buff.mtext);
}
return 0;
}
PROGRAM 10
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/wait.h>
int main()
{
pid_t p;
printf("before fork\n");
p=fork();
if(p==0)
{
printf("i am child having id%d\n",getpid());
printf("my parents id is %d\n",getpid());
}
else
{
wait(NULL);
printf("my child id is %d\n",p);
printf("i am the parent with id %d\n",getpid());
}
printf("Common\n");
return 0;
}