Question

I am supposed to write documentation and report for the code below but I am new...

I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks

#include <pthread.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <ctype.h>

#define handle_error_en(en, msg) \

do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_error(msg) \

do { perror(msg); exit(EXIT_FAILURE); } while (0)

struct thread_info { /* Used as argument to thread_start() */

pthread_t thread_id; /* ID returned by pthread_create() */

int thread_num; /* Application-defined thread # */

char *argv_string; /* From command-line argument */ };

/* Thread start function: display address near top of our stack, and return upper-cased copy of argv_string */

static void * thread_start(void *arg) {

struct thread_info *tinfo = arg;

char *uargv, *p;

printf("Thread %d: top of stack near %p; argv_string=%s\n", tinfo->thread_num, &p, tinfo->argv_string);

uargv = strdup(tinfo->argv_string);

if (uargv == NULL) handle_error("strdup");

for (p = uargv; *p != '\0'; p++)

*p = toupper(*p);

return uargv;

}

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

int s, tnum, opt, num_threads;

struct thread_info *tinfo;

pthread_attr_t attr;

int stack_size;

void *res;

/* The "-s" option specifies a stack size for our threads */

stack_size = -1;

while ((opt = getopt(argc, argv, "s:")) != -1) {

switch (opt) {

case 's': stack_size = strtoul(optarg, NULL, 0);

break;

default: fprintf(stderr, "Usage: %s [-s stack-size] arg...\n", argv[0]);

exit(EXIT_FAILURE);

}

}

num_threads = argc - optind;

/* Initialize thread creation attributes */

s = pthread_attr_init(&attr);

if (s != 0)

handle_error_en(s, "pthread_attr_init");

if (stack_size > 0) {

s = pthread_attr_setstacksize(&attr, stack_size);

if (s != 0)

handle_error_en(s, "pthread_attr_setstacksize");

}

/* Allocate memory for pthread_create() arguments */

tinfo = calloc(num_threads, sizeof(struct thread_info));

if (tinfo == NULL)

handle_error("calloc");

/* Create one thread for each command-line argument */

for (tnum = 0; tnum < num_threads; tnum++) {

tinfo[tnum].thread_num = tnum + 1;

tinfo[tnum].argv_string = argv[optind + tnum];

/* The pthread_create() call stores the thread ID into corresponding element of tinfo[] */

s = pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]);

if (s != 0)

handle_error_en(s, "pthread_create");

}

/* Destroy the thread attributes object, since it is no longer needed */

s = pthread_attr_destroy(&attr);

if (s != 0)

handle_error_en(s, "pthread_attr_destroy");

/* Now join with each thread, and display its returned value */

for (tnum = 0; tnum < num_threads; tnum++) {

s = pthread_join(tinfo[tnum].thread_id, &res);

if (s != 0)

handle_error_en(s, "pthread_join");

printf("Joined with thread %d; returned value was %s\n", tinfo[tnum].thread_num, (char *) res);

free(res); /* Free memory allocated by thread */

}

free(tinfo);

exit(EXIT_SUCCESS);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include

#include

#include

#include

#include

#include

#include

#define handle_error_en(en, msg) \

do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_error(msg) \

do { perror(msg); exit(EXIT_FAILURE); } while (0)

struct thread_info { /* Used as argument to thread_start() */

pthread_t thread_id; /* ID returned by pthread_create() */

int thread_num; /* Application-defined thread # */

char *argv_string; /* From command-line argument */ };

/* Thread start function: display address near top of our stack, and return upper-cased copy of argv_string */

static void * thread_start(void *arg) {

struct thread_info *tinfo = arg;

char *uargv, *p;

printf("Thread %d: top of stack near %p; argv_string=%s\n", tinfo->thread_num, &p, tinfo->argv_string);

uargv = strdup(tinfo->argv_string);

if (uargv == NULL) handle_error("strdup");

for (p = uargv; *p != '\0'; p++)

*p = toupper(*p);

return uargv;

}

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

int s, tnum, opt, num_threads;

struct thread_info *tinfo;

pthread_attr_t attr;

int stack_size;

void *res;

/* The "-s" option specifies a stack size for our threads */

stack_size = -1;

while ((opt = getopt(argc, argv, "s:")) != -1) {

switch (opt) {

case 's': stack_size = strtoul(optarg, NULL, 0);

break;

default: fprintf(stderr, "Usage: %s [-s stack-size] arg...\n", argv[0]);

exit(EXIT_FAILURE);

}

}

num_threads = argc - optind;

/* Initialize thread creation attributes */

s = pthread_attr_init(&attr);

if (s != 0)

handle_error_en(s, "pthread_attr_init");

if (stack_size > 0) {

s = pthread_attr_setstacksize(&attr, stack_size);

if (s != 0)

handle_error_en(s, "pthread_attr_setstacksize");

}

/* Allocate memory for pthread_create() arguments */

tinfo = calloc(num_threads, sizeof(struct thread_info));

if (tinfo == NULL)

handle_error("calloc");

/* Create one thread for each command-line argument */

for (tnum = 0; tnum < num_threads; tnum++) {

tinfo[tnum].thread_num = tnum + 1;

tinfo[tnum].argv_string = argv[optind + tnum];

/* The pthread_create() call stores the thread ID into corresponding element of tinfo[] */

s = pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]);

if (s != 0)

handle_error_en(s, "pthread_create");

}

/* Destroy the thread attributes object, since it is no longer needed */

s = pthread_attr_destroy(&attr);

if (s != 0)

handle_error_en(s, "pthread_attr_destroy");

/* Now join with each thread, and display its returned value */

for (tnum = 0; tnum < num_threads; tnum++) {

s = pthread_join(tinfo[tnum].thread_id, &res);

if (s != 0)

handle_error_en(s, "pthread_join");

printf("Joined with thread %d; returned value was %s\n", tinfo[tnum].thread_num, (char *) res);

free(res); /* Free memory allocated by thread */

}

free(tinfo);

exit(EXIT_SUCCESS);

}

output: Compilation error

Add a comment
Know the answer?
Add Answer to:
I am supposed to write documentation and report for the code below but I am new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h>...

    can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <string.h> #include <dirent.h> #include <sys/wait.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> char *pathLog; char *pathRep;    struct stat attr; int fileNum, curNum; char *create_logPath(char *directory);    void *funcChecker(void *pathSub); void *funcprintTimeAndChanges(void *pathSub);    void main(int argc, char *argv[]) {    if(argc < 3)    { printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]); printf("Typed count is %d\n", argc); printf("Aborted...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT