Skip to content

Commit 39617a2

Browse files
committed
OPERATING SYSTEMS LABORATORY
1 parent fffa047 commit 39617a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+931
-0
lines changed
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<unistd.h>
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
#include<fcntl.h>
5+
6+
int main()
7+
{
8+
int fd[2],n;
9+
char buffer[100];
10+
pid_t p;
11+
pipe(fd);
12+
p = fork();
13+
if(p>0)
14+
{
15+
close(fd[1]);
16+
n = read(fd[0], buffer, 100);
17+
write(1, buffer, n);
18+
19+
}
20+
else
21+
{
22+
close(fd[0]);
23+
printf("Passing values to Parent\n");
24+
write(fd[1],"Hello\n",6);
25+
wait();
26+
27+
}
28+
}
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<stdio.h>
2+
#include<fcntl.h>
3+
#include<unistd.h>
4+
5+
int main()
6+
{
7+
int fd1,fd2,n1,n2;
8+
char buff1[100], buff2[100];
9+
n2 = read(0,buff2,100);
10+
fd2 = open("file2.txt",O_CREAT|O_RDWR,0777);
11+
write(fd2,buff2,n2);
12+
13+
fd1 = open("file1.txt",O_CREAT|O_RDWR,0777);
14+
int off = lseek(fd2,-5,SEEK_END);
15+
read(fd2, buff1, 5);
16+
write(fd1,buff1,5);
17+
printf("Number of characters in file2 is : %d\n",n2);
18+
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<stdio.h>
2+
#include<fcntl.h>
3+
#include<unistd.h>
4+
int main()
5+
{
6+
char buff[100],buff2[100];
7+
8+
int fd = open("file2.txt",O_RDWR,0777);
9+
int n = read(fd,buff,100);
10+
11+
for(int i=n;i>=0;i--)
12+
{
13+
buff2[n-i] = buff[i];
14+
}
15+
write(1,buff2,n+1);
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
#include <pthread.h>
3+
#include <semaphore.h>
4+
void inc();
5+
void dec();
6+
int shared = 1;
7+
sem_t s;
8+
int main()
9+
{
10+
sem_init(&s,0,1); //1st arg: address of semaphore var, 2nd arg: no. of process sharing, 3rd arg: initialisation
11+
pthread_t thread1,thread2;
12+
pthread_create(&thread1, NULL, inc, NULL);
13+
pthread_create(&thread2, NULL, dec, NULL);
14+
pthread_join(thread1,NULL);
15+
pthread_join(thread2,NULL);
16+
}
17+
18+
void inc()
19+
{
20+
int x;
21+
sem_wait(&s);
22+
x=shared;
23+
x++;
24+
sleep(1);
25+
shared = x;
26+
sem_post(&s);
27+
printf("%d\n",shared );
28+
}
29+
void dec()
30+
{
31+
int x;
32+
sem_wait(&s);
33+
x=shared;
34+
x--;
35+
sleep(1);
36+
shared = x;
37+
sem_post(&s);
38+
printf("%d\n",shared );
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<pthread.h>
2+
#include<stdio.h>
3+
#include<unistd.h>
4+
#include<stdlib.h>
5+
int global[2];
6+
7+
void *sum(void *args)
8+
{
9+
int *args_array;
10+
args_array = args;
11+
int n1,n2,sum=0;
12+
n1 = args_array[0];
13+
n2 = args_array[1];
14+
sum = n1+n2;
15+
pthread_exit(sum);
16+
}
17+
18+
void *sub(void *args)
19+
{
20+
int *args_array;
21+
args_array = args;
22+
int n1,n2,sub=0;
23+
n1 = args_array[0];
24+
n2 = args_array[1];
25+
sub = n1-n2;
26+
pthread_exit(sub);
27+
}
28+
29+
int main()
30+
{
31+
pthread_t t1,t2;
32+
printf("Enter Value of n1 : ");
33+
scanf("%d",&global[0]);
34+
printf("\nEnter Value of n2 : ");
35+
scanf("%d",&global[1]);
36+
void *result1, *result2;
37+
38+
pthread_create(&t1,NULL,sum,global);
39+
pthread_create(&t2,NULL,sub,global);
40+
pthread_join(t1,&result1);
41+
pthread_join(t2,&result2);
42+
int n1= result1;
43+
int n2= result2;
44+
printf("\n\n%d", n1*n2);
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include<unistd.h>
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
#include<string.h>
5+
#include<fcntl.h>
6+
int main()
7+
{
8+
FILE *rd;
9+
char buffer[50];
10+
sprintf(buffer, "Rajat");
11+
rd = popen("wc -c","w");
12+
fwrite(buffer, sizeof(char), strlen(buffer), rd);
13+
pclose(rd);
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include<stdio.h>
2+
#include<unistd.h>
3+
#include<fcntl.h>
4+
int main()
5+
{
6+
int fd,n;
7+
fd = open("file2.txt",O_RDONLY);
8+
char buff[100];
9+
int k = lseek(fd,10,SEEK_SET);
10+
n = read(fd,buff,15);
11+
write(1,buff,n);
12+
}

0 commit comments

Comments
 (0)