lab 8 code/m1_test2.c #include #include #include #include #include #include #include #include #include int msg_count = 0; pthread_mutex_t mutex; static void new_msg(union sigval sv){ struct mq_attr...

1 answer below »
Can you guys do this in time ?


lab 8 code/m1_test2.c #include #include #include #include #include #include #include #include #include int msg_count = 0; pthread_mutex_t mutex; static void new_msg(union sigval sv){ struct mq_attr attr; ssize_t size; char *buf; mqd_t queue = *((mqd_t *) sv.sival_ptr); // Determine max. msg size; allocate buffer to receive msg if (mq_getattr(queue, &attr)){ perror("mq_getattr\n"); exit(-1); } buf = malloc(attr.mq_msgsize); if (buf == NULL){ perror("malloc"); exit(-1); } size = mq_receive(queue, buf, attr.mq_msgsize, NULL); if (size == -1){ perror("mq_receive\n"); exit(-1); } pthread_mutex_lock(&mutex); printf("Received message \"%s\"\n", buf); fflush(stdout); free(buf); msg_count ++; pthread_mutex_unlock(&mutex); } int main(int argc, char** argv){ mqd_t msg_queue = mq_open("/ITF22519-Queue", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP, NULL); if(msg_queue == -1) { perror("mq_open\n"); return -1; } // Determine max. msg size; allocate buffer to receive msg struct mq_attr attr; char *buf; if (mq_getattr(msg_queue, &attr)){ perror("mq_getattr\n"); exit(-1); } buf = malloc(attr.mq_msgsize); if (buf == NULL){ perror("malloc"); exit(-1); } sleep(10); ssize_t size; size = mq_receive(msg_queue, buf, attr.mq_msgsize, NULL); if (size == -1){ perror("mq_receive\n"); exit(-1); } printf("Received message \"%s\"\n", buf); size = mq_receive(msg_queue, buf, attr.mq_msgsize, NULL); if (size == -1){ perror("mq_receive\n"); exit(-1); } printf("Received message \"%s\"\n", buf); free(buf); char my_string[] = "My name is Sara"; if( mq_send(msg_queue, my_string, strlen(my_string), 12)){ perror("mq_send\n"); return -1; } return 0; } lab 8 code/mq_test1.c #include #include #include #include #include #include #include #include int main(int argc, char** argv){ mqd_t msg_queue = mq_open("/ITF22519-Queue", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP, NULL); if(msg_queue == -1){ perror("mq_open\n"); return -1; } char my_string1[] = "I am the third year student."; char my_string2[] = "I am the second year student."; sleep(5); if( mq_send(msg_queue, my_string1, strlen(my_string1), 27)){ perror("mq_send"); return -1; } if( mq_send(msg_queue, my_string2, strlen(my_string2), 42)){ perror("mq_send"); return -1; } // Determine max. msg size; allocate buffer to receive msg struct mq_attr attr; char *buffer; if (mq_getattr(msg_queue, &attr)){ perror("mq_getattr\n"); exit(-1); } buffer = malloc(attr.mq_msgsize); if (buffer == NULL){ perror("malloc"); exit(-1); } sleep(10); int priority; size_t size = mq_receive(msg_queue, buffer, attr.mq_msgsize, &priority); if( size == -1){ perror("mq_receive"); return -1; } printf("Received message \"%s\"\n", buffer); mq_unlink("/ITF22519-Queue"); return 0; } lab 8 code/pipe_test.c #include #include #include #include #define READ_END_OF_FILE 0 #define WRITE_END_OF_FILE 1 int main(int argc, char** argv) { int my_pipe[2]; // Create a pipe if(pipe(my_pipe)){ perror("Failed to create pipe\n"); return -1; } pid_t child = fork(); if(child == 0){// This is a child process close(my_pipe[READ_END_OF_FILE]); FILE* out = fdopen(my_pipe[WRITE_END_OF_FILE], "w"); // open up a file, associate a stream with a file descriptor sleep(2); fprintf(out, "Are you my parent?"); // print something return 42; } else if(child > 0){// This is a parent process close(my_pipe[WRITE_END_OF_FILE]); FILE* in = fdopen(my_pipe[READ_END_OF_FILE], "r"); // open up as a file stream, associate a stream with a file descriptor char buffer[100]; fgets(buffer, 100, in); printf("My child asked \"%s\"\n", buffer); int status; wait(&status); printf("And then returned %d\n", WEXITSTATUS(status)); } else{ perror("Failed to fork\n"); return -1; } return 0; } lab 8 code/shm_test.h #ifndef SHM_TEST_H #define SHM_TEST_H struct SHM_SHARED_MEMORY { char a_string[100]; int an_array[5]; char* a_ptr; }; #endif lab 8 code/shm_test1.c #include #include #include #include #include #include #include #include "shm_test.h" int main(int argc, char** argv){ int fd; fd = shm_open("/ITF22519LabsIPC", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); // open the shared memory area, create it if it doesn't exist if(!fd){ perror("shm_open\n"); return -1; } if(ftruncate(fd, sizeof(struct SHM_SHARED_MEMORY))){ perror("ftruncate\n"); return -1; } struct SHM_SHARED_MEMORY* shared_mem; shared_mem = mmap(NULL, sizeof(struct SHM_SHARED_MEMORY), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(!shared_mem){ perror("mmap\n"); return -1; } if(close(fd)){ perror("close\n"); return -1; } int i; for(i = 0; i < 5;="" i++){="" shared_mem-="">an_array[i] = i*i; } char my_string[] = "I am a string allocated on main's stack!"; shared_mem->a_ptr = my_string; sleep(5); printf("a_string = \"%s\"\n", shared_mem->a_string); printf("an_array[] = {%d, %d, %d, %d, %d}\n", shared_mem->an_array[0], shared_mem->an_array[1], shared_mem->an_array[2], shared_mem->an_array[3], shared_mem->an_array[4]); if(shared_mem->a_ptr > 0){ printf("a_ptr = %lu = \"%s\"\n", shared_mem->a_ptr, shared_mem->a_ptr); } else{ printf("a_ptr = NULL\n"); } } lab 8 code/shm_test2.c #include #include #include #include #include #include #include #include #include "shm_test.h" int main(int argc, char** argv){ int fd; fd = shm_open("/ITF22519LabsIPC", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); // open the shared memory area, create it if it doesn't exist if(!fd){ perror("shm_open\n"); return -1; } if(ftruncate(fd, sizeof(struct SHM_SHARED_MEMORY))){ perror("ftruncate\n"); return -1; } struct SHM_SHARED_MEMORY* shared_mem; shared_mem = mmap(NULL, sizeof(struct SHM_SHARED_MEMORY), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(!shared_mem){ perror("mmap\n"); return -1; } if(close(fd)){ perror("close\n"); return -1; } strcpy(shared_mem->a_string, "I am a buffer in the shared memory area"); shared_mem->an_array[0] = 42; sleep(5); printf("a_string = \"%s\"\n", shared_mem->a_string); printf("an_array[] = {%d, %d, %d, %d, %d}\n", shared_mem->an_array[0], shared_mem->an_array[1], shared_mem->an_array[2], shared_mem->an_array[3], shared_mem->an_array[4]); fflush(stdout); if(shared_mem->a_ptr > 0){ printf("a_ptr = %lu = \"%s\"\n", shared_mem->a_ptr, shared_mem->a_ptr); } else{ printf("a_ptr = NULL\n"); } } lab 8 code/sig_fork.c #include #include #include #include #include #include pid_t pid; int count = 0; void handler1(int sig){ count++; printf("count = %d in handler 1\n", count); fflush(stdout); kill(pid, SIGUSR1); } void handler2(int sig){ count += 10; printf("count = %d in handler 2\n", count); exit(0); } int main() { pid_t p; int status; signal(SIGUSR1, handler1); if ((pid = fork()) == 0){ signal(SIGUSR1, handler2); kill(getppid(), SIGUSR1); while (1); } if ((p = wait(&status)) > 0){ count += 100; printf("count = %d in main hander\n", count); } return 0; } lab 8 code/sig_test.c #include #include #include #include #include void my_quit_handler(int i) { printf("\nAha. You are trying to quit but it is not that simple ~~~~ \n"); fflush(stdout); } int main(int argc, char** argv) { signal(SIGINT, my_quit_handler); while(1) { printf("."); fflush(stdout); sleep(1); } return 0; } lab 9 code/fcfs.c #include "scheduling.h" void first_come_first_served(process_t *proc) { printf("\n\nFirst come first served\n"); int tick = 0; //simulated CPU clock process_t* current_process = proc; while(1) { if(current_process == NULL) break; //If the current process already arrived if(current_process->arrivaltime <= tick)="" {="" if="" it="" is="" not="" running,="" run="" it="" if(!current_process-="">isRunning) { current_process->isRunning = 1; current_process->starttime = tick; } else //the process already started { current_process->remainingtime--; //if the process is done if(current_process->remainingtime <= 0)="" {="" current_process-="">isDone = 1; current_process->isRunning = 0; current_process->endtime = tick; //Update to the next process current_process = current_process->next; //if the next process can start now if(current_process != NULL && current_process->arrivaltime <= tick)="" {="" current_process-="">isRunning = 1; current_process->starttime = tick; } } } } tick++; } } lab 9 code/makefile CC = gcc OBJS = scheduling.o prio.o srt.o fcfs.o rr.o DEPS = scheduling.h CFLAGS = -g -Wall LIBRARIES = -lrt -lpthread all: scheduling scheduling: $(OBJS) $(DEPS) $(CC) $(LIBRARIES) $(CFLAGS) -o $@ $^ .o: %.c $(DEPS) $(CC) $(LIBRARIES) $(CFLAGS)-o $@ -c $< clean:="" rm="" -rf="" *.o="" *.out="" scheduling="" lab="" 9="" code/prio.c="" #include="" "scheduling.h"="" void="" priority(process_t="" *proc)="" {="" printf("\n\npriority="" (premptive)\n");="" todo:="" implement="" scheduling="" algorithm="" here="" }="" lab="" 9="" code/processes1.in="" process="" arrival="" runtime="" priority="" 0="" 10="" 25="" 0="" 1="" 69="" 36="" 2="" 2="" 87="" 20="" 0="" 3="" 1="" 16="" 2="" 4="" 46="" 28="" 0="" 5="" 92="" 14="" 1="" 6="" 74="" 12="" 1="" 7="" 61="" 28="" 0="" 8="" 89="" 27="" 0="" 9="" 28="" 31="" 1="" 10="" 34="" 33="" 2="" 11="" 82="" 13="" 1="" 12="" 93="" 32="" 0="" 13="" 85="" 33="" 0="" 14="" 87="" 11="" 1="" 15="" 57="" 35="" 1="" 16="" 2="" 10="" 0="" 17="" 27="" 31="" 0="" 18="" 34="" 10="" 0="" 19="" 78="" 18="" 1="" lab="" 9="" code/processes2.in="" process="" arrival="" runtime="" priority="" 0="" 0="" 10="" 0="" 1="" 2="" 20="" 2="" 2="" 9="" 50="" 0="" lab="" 9="" code/rr.c="" #include="" "scheduling.h"="" void="" round_robin(process_t="" *proc)="" {="" printf("\n\nround="" robin\n");="" int="" quantum="4;" todo:="" implement="" scheduling="" algorithm="" here="" }="" lab="" 9="" code/scheduling.c="" #include="" "scheduling.h"="" read="" the="" processes="" from="" a="" file="" process_t*="" read_processes_from_file(char="" filename[])="" {="" file*="" pfile="fopen" (filename,"r");="" if="" (pfile="=NULL)" {="" perror="" ("error="" opening="" file");="" return="" null;="" }="" fscanf(pfile,="" "%*[^\n]");="" read="" and="" discard="" first="" line="" int="" id,="" arrival,="" runtime,="" priority;="" process_t="" *head_process="NULL," *prev_process="NULL," *new_process="NULL;" while(fscanf(pfile,="" "%d%d%d%d",="" &id,="" &arrival,="" &runtime,="" &priority)="" !="EOF)" {="" new_process="malloc(sizeof(process_t));" new_process-="">id = id; new_process->arrivaltime = arrival; new_process->runtime = runtime; new_process->priority = priority; new_process->next = NULL; new_process->remainingtime = runtime; new_process->isRunning = 0; new_process->isDone = 0; /*printf("%d\t%d\t%d\t%d\n", new_process->id, new_process->arrivaltime, new_process->runtime, new_process->priority);*/ if(prev_process == NULL) prev_process = new_process; else { prev_process->next = new_process; prev_process = new_process; } if(head_process == NULL) head_process = new_process; } return head_process; } //print the scheduled processes to file and terminal void print_processes(process_t* processes, char* filename) { FILE* pFile = fopen (filename,"w"); if (pFile==NULL) { perror ("Error creating file"); return; } process_t* current_process = processes; printf("Process arrival runtime priority starttime endtime\n"); fprintf(pFile, "Process arrival runtime priority starttime endtime\n"); int process_count = 0; float avg_turnaround = 0; float avg_weighted_turnaround = 0; while(current_process != NULL) { printf("%d\t%d\t%d\t%d\t%d\t%d\n", current_process->id, current_process->arrivaltime, current_process->runtime, current_process->priority, current_process->starttime, current_process->endtime); fprintf(pFile, "%d\t%d\t%d\t%d\t%d\t%d\n", current_process->id, current_process->arrivaltime, current_process->runtime, current_process->priority, current_process->starttime, current_process->endtime); process_count++; int turnarround = current_process->endtime - current_process->arrivaltime; float weighted_turnaround = turnarround * 1.0/current_process->runtime; avg_turnaround += turnarround; avg_weighted_turnaround += weighted_turnaround; current_process = current_process->next; } avg_turnaround = avg_turnaround/process_count; avg_weighted_turnaround = avg_weighted_turnaround/process_count; printf("Average turnarround = %f\n", avg_turnaround); printf("Average weighted turnarround = %f\n", avg_weighted_turnaround); } //sort the processes based on their arrival time //Ref: Bubble sort https://en.wikipedia.org/wiki/Bubble_sort process_t* sort_process(process_t *proc) { int sorted = 0; process_t *head = proc; while(!sorted) { sorted = 1; process_t *prev, *cur, *temp; cur = head; prev = cur; while(cur!= NULL && cur->next != NULL) { if(cur->arrivaltime > cur->next->arrivaltime) { sorted = 0; if(prev != cur) { temp = cur->next; prev->next = temp; cur->next = temp->next; temp->next = cur; } else { temp = cur->next; cur->next = temp->next; temp->next = cur; head = temp; } } prev = cur; cur = cur->next; } } return head; } void delete_processes(process_t *proc) { //TODO: free the memory allocated for this linked list proc } int main(int argc
Answered Same DayNov 03, 2021

Answer To: lab 8 code/m1_test2.c #include #include #include #include #include #include #include #include...

Ria answered on Nov 12 2021
136 Votes
lab7_report.txt
Excercise 1:
1. What is the expected output of the above program?
Hello World from Thread #0, count = 0!
Hello World from Thread #0, count = 1!
Hello World from Thread #0, count = 2!
Hello World from Thread #0, count = 3!
Hello World from Thread #0, count = 4!
Hello World from Thread #1, count = 5!
Hello World from Thread #1, count = 6!
Hello World from Thread #1, count = 7!
Hello Worl
d from Thread #1, count = 8!
Hello World from Thread #1, count = 9!
Hello World from Thread #2, count = 10!
Hello World from Thread #2, count = 11!
Hello World from Thread #2, count = 12!
Hello World from Thread #2, count = 13!
Hello World from Thread #2, count = 14!
Hello World from Thread #3, count = 15!
Hello World from Thread #3, count = 16!
Hello World from Thread #3, count = 17!
Hello World from Thread #3, count = 18!
Hello World from Thread #3, count = 19!
Hello World from Thread #4, count = 20!
Hello World from Thread #4, count = 21!
Hello World from Thread #4, count = 22!
Hello World from Thread #4, count = 23!
Hello World from Thread #4, count = 24!
2. Run the code and explain the output.
Hello World from Thread #0, count = 0!
Hello World from Thread #2, count = 0!
Hello World from Thread #1, count = 0!
Hello World from Thread #3, count = 0!
Hello World from Thread #4, count = 1!
Hello World from Thread #4, count = 5!
Hello World from Thread #3, count = 5!
Hello World from Thread #1, count = 5!
Hello World from Thread #2, count = 5!
Hello World from Thread #0, count = 5!
Hello World from Thread #0, count = 10!
Hello World from Thread #2, count = 10!
Hello World from Thread #1, count = 10!
Hello World from Thread #3, count = 10!
Hello World from Thread #4, count = 10!
Hello World from Thread #2, count = 15!
Hello World from Thread #1, count = 15!
Hello World from Thread #0, count = 15!
Hello World from Thread #4, count = 15!
Hello World from Thread #3, count = 15!
Hello World from Thread #3, count = 20!
Hello World from Thread #2, count = 20!
Hello World from Thread #4, count = 20!
Hello World from Thread #0, count = 20!
Hello World from Thread #1, count = 20!
3. What caused the discrepancy between the expected and real outputs?
----------------------------------------------------------------------------------------------------------------------------
Exercise 2:
#include
#include
#include
#include
#include
#include
#define NUM_OF_THREADS 10
int count = 0;
pthread_mutex_t lock;
void* PrintMessage(void* ThreadId) {
    
    pthread_mutex_lock(&lock);
    long tid;
    int i;
    tid = (long)ThreadId;
    for (i = 0; i < 5; i++) {
        printf("Hello World from Thread #%ld, count = %d!\n", tid, count);
        count++;
        sleep(2);
    }
    
    pthread_mutex_unlock(&lock);
}
int main(int argc, char* argv[]) {
    
    pthread_t threads[NUM_OF_THREADS];
    int ret;
    long i;
    
    if (pthread_mutex_init(&lock, NULL) != 0) {
printf("\nMutex init has failed\n");
return 1;
}
    pthread_create(&threads[0], NULL, PrintMessage, (void*)0);
    pthread_create(&threads[1], NULL, PrintMessage, (void*)1);
    pthread_create(&threads[2], NULL, PrintMessage, (void*)2);
    pthread_create(&threads[3], NULL, PrintMessage, (void*)3);
    pthread_create(&threads[4], NULL, PrintMessage, (void*)4);
    pthread_join(threads[0], NULL);
    pthread_join(threads[1], NULL);
    pthread_join(threads[2], NULL);
    pthread_join(threads[3], NULL);
    pthread_join(threads[4], NULL);
    
    pthread_mutex_destroy(&lock);
    
    return 0;
}
----------------------------------------------------------------------------------------------------------------------------
Exercise 3:
1. Try to analyze above code without running and estimate what the expected output would be.
I am Thread 1
Hello World from Thread #1, count = 0!
I am Thread 1
Hello World from Thread #1, count = 1!
I am Thread 1
Hello World from Thread #1, count = 2!
I am Thread 1
Hello World from Thread #1, count = 3!
I am Thread 1
Hello World from Thread #1, count = 4!
I am Thread 1
Hello World from Thread #1, count = 5!
I am...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here