I3MUP1+Exercise+7

toc =Exercise 7=

=7.1 Write a Linux-edition API=

The following classes have been implemented in our API:

//Has the sleep(long int ms) function that puts the program to sleep for ms miliseconds.// code format="cpp" void SleepTimer::sleep(long int ms) {   usleep(ms * 1000); } code //Below you will see the implementation of the Thread class// code format="cpp" void Thread::start {   pthread_attr_t attr; sched_param sched_p;
 * ==SleepTimer==
 * ==Thread==

pthread_attr_init(&attr);                                       // Init attr pthread_attr_setschedpolicy(&attr, SCHED_RR);                   // Set RR scheduling (RT, timesliced) pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);   // Create thread with explicit (non-inherited) scheduling - setting priority will not work otherwise! sched_p.sched_priority = (int)priority;                           // Set priority pthread_attr_setschedparam(&attr, &sched_p);                   // Use the priority

pthread_create(&threadId, &attr, threadMapper, (void*)this); }

bool Thread::setPriority(ThreadPriority pri) {   priority = pri; if ( pthread_setschedprio(threadId, priority) == 0 ) return 1; else return false; }

Thread::ThreadPriority Thread::getPriority {   return priority; }

string Thread::getName {   return name; }

void * Thread::threadMapper(void * p) { ((Thread *)p)->run; return NULL; } code

//Uses the semaphore library for its functionality. Implementation shown below:// code format="cpp" void CountingSemaphore::wait {   sem_wait(&sem); }
 * ==CountingSemaphore==

void CountingSemaphore::signal {   sem_post(&sem); } code

//This class uses the pthread library. wait locks the mutex and signal releases it again. Implementation of relevant functions is shown below:// code format="cpp" void Mutex::wait {   pthread_mutex_lock(&m); }
 * ==Mutex==

void Mutex::signal {   pthread_mutex_unlock(&m); } code

//The ScopedLocker class uses our own Mutex class and the constructor passes a Mutex object as arguement. Implementation below:// code format="cpp" ScopedLocker::ScopedLocker(Mutex & mutex) : m(mutex) {   m.wait; }
 * ==ScopedLocker==

ScopedLocker::~ScopedLocker {   m.signal; } code

//System V message queues were used for the implementation of our Mailbox. Implementation is shown below:
 * ==Mailbox==

code format="cpp" template class Mailbox { public: Mailbox(const long int cap) {       capacity = cap * sizeof(Item); struct msqid_ds * buf;

msgqID = msgget((key_t) IPC_PRIVATE, 0666 | IPC_CREAT);

msgctl(msgqID, IPC_STAT, buf); buf->msg_qbytes = capacity; msgctl(msgqID, IPC_SET, buf); }

~Mailbox {       msgctl(msgqID, IPC_RMID, NULL); }

void put(const Item& z)   { msgStruct myMsg; myMsg.data = z;       msgsnd(msgqID, (void*)&myMsg, sizeof(myMsg.data), 0);

}

Item get {       msgStruct myMsg; msgrcv(msgqID, (void*)&myMsg, sizeof(myMsg.data), 0, 0); return myMsg.data; }

private: long int capacity; int msgqID; struct msgStruct {       msgStruct {           type = 1; }       long int type; Item data; };

}; code

=7.2 Test the API= //Create test applications as necessary to test your OS API. Pay particular attention to the test of resources that are implemented using templates, since they are not compiled until they are instantiated.//

Using the handout test program we tested our new API. Everything worked as intended.

=7.3 Create the Library Files= By following the procedure in "Fremstilling af biblioteker til abstrakt, objektorienteret OS API" we managed to create the .lib files for our new linux API.

=7.4-5 Port your solution to Lab Exercises 6.1 and 6.2=

We redid Lab exercise 6.2 and 6.3 by simply including "linuxwrap.h" instead of "win32wrap.h". We were unable to actually run our program through eclipse but navigating to - and opening the program using "sudo" solved that problem.