Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) What might have been the intention of the author of this program? (b) What h

ID: 3692917 • Letter: #

Question

(a) What might have been the intention of the author of this program?

(b) What happens if you run this program? How could we fix these problems?

(c) What could happen if you run the version debugged in part (b) using a thread library based on the m-to-1 threading model?

#include #include #include #de fine READ END 0 #de fine WRITE END 1 #de fine MAXBUFSIZ 100 int mypipe [2]: static void *thread func (void* arg) char buffer [MAXBUFSIZ]; close (mypipe [READ_END]); sprintf (buffer, "I am the child thread "); write (mypipe [WRITE_END], buffer, strlen (buffer)); close (mypipe [WRITE_END]); return (void *)NULL; main () pthread t tid; int len; char buffer [MAXBUFSIZ]; pipe (mypipe); pthread create (&tid;, NULL, thread func, NULL) : close (mypipe [WRITE_END]); read (mypipe [READ_END], buffer, MAXBUFSIZ); printf("%s ", buffer); close (mypipe [READ_END]);

Explanation / Answer

a)answer:

It is a one way communication between parent and child process. Here child thread can write a message into buffer but it can not read anything from the buffer. Parent thread can read the child thread’s message from the buffer but it can not write anything into buffer.

b) answer:

In function 'void* thread_func(void*)':

13:27: error: 'close' was not declared in this scope

15:49: error: 'strlen' was not declared in this scope

15:50: error: 'write' was not declared in this scope At global scope:

19:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] In function 'int main()':

25:16: error: 'pipe' was not declared in this scope

27:28: error: 'close' was not declared in this scope

28:44: error: 'read' was not declared in this scope 22:9: warning: unused variable 'len' [-Wunused-variable].

For fix these errors:

Replace #include<string.h> in place #include<strings.h>

Include header file called, #include<unistd.h>

code::

#include<pthread.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#define READ_END 0
#define WRITE_END 1
#define MAXBUFSIZE 100
int mypipe[2];
static void *thread_func(void* arg)
{
char buffer[MAXBUFSIZE];
//close(mypipe[READ_END]);
sprintf(buffer,"I am the child thread ");
write(mypipe[WRITE_END],buffer,strlen(buffer));
close(mypipe[WRITE_END]);
return (void *)NULL;
}
main()
{
pthread_t tid;
int len,i;
char buffer[MAXBUFSIZE];
  
pipe(mypipe);
pthread_create(&tid,NULL,thread_func,NULL);
//close(mypipe[WRITE_END]);
read(mypipe[READ_END],buffer,MAXBUFSIZE);
printf("%s ",buffer);
close(mypipe[READ_END]);
}

output::

I am the child thread

Exit code: 0 (normal program termination)

c) answer:

#include<pthread.h>

#include<stdio.h>

#include<string.h>

#include<unistd.h>

#define READ_END 0

#define WRITE_END 1

#define MAXBUFSIZE 100

int mypipe[2];

static void *thread_func(void* arg)

{

    char buffer[MAXBUFSIZE];

    //static int pid=0;

    //pid++;

    //close(mypipe[READ_END]);

    sprintf(buffer,"I am the child thread ");

    write(mypipe[WRITE_END],buffer,strlen(buffer));

    //pid=pid+1;

    close(mypipe[WRITE_END]);

    return (void *)NULL;

}

main()

{

    pthread_t tid;

    int len,i;

    char buffer[MAXBUFSIZE];

   

    pipe(mypipe);

//many to one thread model(m-to-1)

for(i=0;i<5;i++){

    pthread_create(&tid,NULL,thread_func,NULL);

    //close(mypipe[WRITE_END]);

    read(mypipe[READ_END],buffer,MAXBUFSIZE);

    printf("%s ",buffer);

    close(mypipe[READ_END]);}

}

Output:::

I am the child thread

I am the child thread

I am the child thread

I am the child thread

I am the child thread

Exit code: 0 (normal program termination)