0% found this document useful (0 votes)
50 views11 pages

Micro

The document describes a program that creates two threads using pthread_create. It defines an array tid to store the thread IDs. A function doSomeThing is defined to be the thread target function. In main, a while loop iterates to create two threads by calling pthread_create and storing the IDs in tid array. It prints messages to identify which thread is processing. After creating threads, it waits for 5 seconds before exiting.

Uploaded by

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

Micro

The document describes a program that creates two threads using pthread_create. It defines an array tid to store the thread IDs. A function doSomeThing is defined to be the thread target function. In main, a while loop iterates to create two threads by calling pthread_create and storing the IDs in tid array. It prints messages to identify which thread is processing. After creating threads, it waits for 5 seconds before exiting.

Uploaded by

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

Program:6

#include<stdio.h>

int mutex = 1, full = 0, empty = 3, x = 0;

int wait(int);

int signal(int);

void producer();

void consumer();

int main() {

int n;

printf("\n1. PRODUCER\n2. CONSUMER\n3. EXIT\n");

while (1) {

printf("\nENTER YOUR CHOICE: ");

scanf("%d", &n);

switch (n) {

case 1:

if ((mutex == 1) && (empty != 0))

producer();

else

printf("BUFFER IS FULL");

break;

case 2:

if ((mutex == 1) && (full != 0))

consumer();

else

printf("BUFFER IS EMPTY");

break;

case 3:

exit(0);

break;

}}}

int wait(int s) {

return (--s);

int signal(int s) {
return (++s);

void producer() {

mutex = wait(mutex);

full = signal(full);

empty = wait(empty);

x++;

printf("\nProducer produces the item %d", x);

mutex = signal(mutex);

void consumer() {

mutex = wait(mutex);

full = wait(full);

empty = signal(empty);

printf("\nConsumer consumes item %d", x);

x--;

mutex = signal(mutex);

}
Program:7
#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include<sys/types.h>

#define SEGSIZE 100

int main(int argc, char *argv[]) {

int shmid, cntr;

key_t key;

char *segptr;

char buff[] = "poooda";

key = ftok(".", 's');

if ((shmid = shmget(key, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666)) == -1) {

if ((shmid = shmget(key, SEGSIZE, 0)) == -1) {

perror("shmget");

exit(1);

} else {

printf("Creating a new shared memory segment\n");

printf("SHMID: %d\n", shmid);

system("ipcs -m");

if ((segptr = (char *)shmat(shmid, 0, 0)) == (char *)-1) {

perror("shmat");

exit(1);

printf("Writing data to shared memory...\n");

strcpy(segptr, buff);

printf("DONE\n");

printf("Reading data from shared memory...\n");

printf("DATA: %s\n", segptr);


printf("DONE\n");

printf("Removing shared memory segment...\n");

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

printf("Can't Remove Shared memory Segment...\n");

else

printf("Removed Successfully\n");

return 0;

}
Program: 8
#include<stdio.h>

#include<conio.h>

int max[100][100];

int alloc[100][100];

int need[100][100];

int avail[100];

int n, r;

void input();

void show();

void cal();

int main() {

int i, j;

printf("**** Banker's Algorithm ****\n");

input();

show();

cal();

getch();

return 0;

void input() {

int i, j;

printf("Enter the number of Processes\t");

scanf("%d", &n);

printf("Enter the number of resources instances\t");

scanf("%d", &r);

printf("Enter the Max Matrix\n");

for(i = 0; i < n; i++) {

for(j = 0; j < r; j++) {

scanf("%d", &max[i][j]);

printf("Enter the Allocation Matrix\n");

for(i = 0; i < n; i++) {


for(j = 0; j < r; j++) {

scanf("%d", &alloc[i][j]);

} }

printf("Enter the available Resources\n");

for(j = 0; j < r; j++) {

scanf("%d", &avail[j]);

}}

void show() {

int i, j;

printf("Process\t Allocation\t Max\t Available\t");

for(i = 0; i < n; i++) {

printf("\nP%d\t ", i + 1);

for(j = 0; j < r; j++) {

printf("%d ", alloc[i][j]);

printf("\t");

for(j = 0; j < r; j++) {

printf("%d ", max[i][j]);

printf("\t");

if(i == 0) {

for(j = 0; j < r; j++) {

printf("%d ", avail[j]);

}}}}

void cal() {

int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;

int safe[100];

int i, j;

for(i = 0; i < n; i++) {

finish[i] = 0;

} for(i = 0; i < n; i++) {

for(j = 0; j < r; j++) {

need[i][j] = max[i][j] - alloc[i][j];

} }
8(3)
printf("\n");

while(flag) {

flag = 0;

for(i = 0; i < n; i++) {

int c = 0;

for(j = 0; j < r; j++) {

if((finish[i] == 0) && (need[i][j] <= avail[j])) {

c++;

if(c == r) {

for(k = 0; k < r; k++) {

avail[k] += alloc[i][j];

finish[i] = 1;

flag = 1; }

printf("P%d->", i);

if(finish[i] == 1) {

i = n;

}}}}}

for(i = 0; i < n; i++) {

if(finish[i] == 1) {

c1++;

} else {

printf("P%d->", i);

}}

if(c1 == n) {

printf("\nThe system is in a safe state");

} else {

printf("\nProcesses are in a deadlock");

printf("\nSystem is in an unsafe state");

}}
Program: 9
#include<stdio.h>

#include<conio.h>

int max[100][100];

int alloc[100][100];

int need[100][100];

int avail[100];

int n, r;

void input();

void show();

void cal();

int main() {

int i, j;

printf("********** Deadlock Detection Algorithm ************\n");

input();

show();

cal();

getch();

return 0;

void input() {

int i, j;

printf("Enter the number of processes: ");

scanf("%d", &n);

printf("Enter the number of resource instances: ");

scanf("%d", &r);

printf("Enter the Max Matrix\n");

for(i = 0; i < n; i++) {

for(j = 0; j < r; j++) {

scanf("%d", &max[i][j]);

} }

printf("Enter the Allocation Matrix\n");

for(i = 0; i < n; i++) {

for(j = 0; j < r; j++) {

scanf("%d", &alloc[i][j]);
}}

printf("Enter the available Resources\n");

for(j = 0; j < r; j++) {

scanf("%d", &avail[j]);

}}

void show() {

int i, j;

printf("Process\t Allocation\t Max\t Available\n");

for(i = 0; i < n; i++) {

printf("P%d\t ", i+1);

for(j = 0; j < r; j++) {

printf("%d ", alloc[i][j]);

printf("\t");

for(j = 0; j < r; j++) {

printf("%d ", max[i][j]);

printf("\t");

if(i == 0) {

for(j = 0; j < r; j++)

printf("%d ", avail[j]);

printf("\n");

void cal() {

int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;

int dead[100], safe[100];

int i, j;

for(i = 0; i < n; i++) {

finish[i] = 0;

for(i = 0; i < n; i++) {

for(j = 0; j < r; j++) {

need[i][j] = max[i][j] - alloc[i][j];


9(3)
}}

while(flag) {

flag = 0;

for(i = 0; i < n; i++) {

int c = 0;

for(j = 0; j < r; j++) {

if((finish[i] == 0) && (need[i][j] <= avail[j])) {

c++;

if(c == r) {

for(k = 0; k < r; k++) {

avail[k] += alloc[i][j];

finish[i] = 1;

flag = 1;

printf("P%d->", i);

if(finish[i] == 1) {

i = n;

}}}}}}

j = 0;

flag = 0;

for(i = 0; i < n; i++) {

if(finish[i] == 0) {

dead[j] = i;

j++;

flag = 1;

if(flag == 1) {

printf("\n\nSystem is in Deadlock and the Deadlock process are\n");

for(i = 0; i < n; i++) {

printf("P%d\t", dead[i]);

} else {

printf("\nNo Deadlock Occurs");


}}

Program: 10
#include<stdio.h>

#include<string.h>

#include<pthread.h>

#include<stdlib.h>

#include<unistd.h>

pthread_t tid[2]; // Array to store thread IDs

void* doSomeThing(void *arg) {

unsigned long i = 0;

pthread_t id = pthread_self();

if(pthread_equal(id, tid[0])) {

printf("\nFirst thread processing\n");

} else {

printf("\nSecond thread processing\n");

for(i = 0; i < (0xFFFFFFFF); i++);

return NULL;

int main(void) {

int i = 0;

int err;

while(i < 2) {

err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); // Create thread

if (err != 0)

printf("\nCan't create thread :[%s]", strerror(err));

else

printf("\nThread created successfully\n");

i++;

sleep(5);

return 0;

You might also like