Operating System Laboratory Assignment
Operating System Laboratory Assignment
REGISTRATION NUMBER:11816118
ROLL NO: 18
SECTION - MS
QUESTION NO- 4
QUESTION 1
#include <stdio.h>
#include <pthread.h>
#include"mythreads.h"
void lock_init()
{
flag[0] = flag[1] = 0;
turn = 0;
}
void lock(int self)
{ flag[self] =
1;
turn = 1-self;
lock(self);
unlock(self); }
int main() {
pthread_t p1, p2;
lock_init();
pthread_join(p1, NULL);
pthread_join(p2, NULL);
return 0;
void Pthread_mutex_lock(pthread_mutex_t *m)
{
int rc = pthread_mutex_lock(m);
assert(rc == 0);
}
OUTPUT
QUESTION NO 3
#include<iostr
eam>
#include<thread>
#include<mutex>
using namespace
std; std::mutex m1;
std::mutex m2;
std::mutex m3; void
thread1()
{ m1.lock();
m2.lock(); m3.lock();
cout<<"Critical section of Thread Thread
One\n"; m1.unlock();
m2.unlock();m3.unlock();
}
void thread2()
{ m2.lock();
m1.lock(); m3.lock();
cout<<"Critical section of Thread Thread
Two\n"; m2.unlock(); m1.unlock();
m3.unlock();
}
void thread3()
{ m3.lock();
m1.lock(); m2.lock();
cout<<"Critical section of Thread Thread
Three\n"; m3.unlock(); m1.unlock();
m2.unlock(); }
int main() {
thread
t1(thread1);
thread
t2(thread2);
thread
t3(thread3);
t1.join(); t2.join();
t3.join(); return 0;
}
QUESTION NO 4
fp = fopen(“test.txt”,”r”); if (fp==
NULL) { puts(“cannot open this
file”); exit(1); } feek(fp, 0,
SEEK_END); length = ftell(fp);
fseek(fp, (length-5), SEEK_SET);
do{
ch = fgetc(fp);
putchar(ch); }while
(ch !=EOF);
fclose(fp);
return(0);
}