How would I go about using the sterror funciton in my program? -----------------
ID: 3587862 • Letter: H
Question
How would I go about using the sterror funciton in my program?
------------------------------------------------------------------------------------------
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include<pthread.h>
#include<errno.h>
#include <stdlib.h>
int value;
int pid;
pthread_t tid;
void *myThreadFun(void *vargp)
{
printf("Second thread: My TID is %d, still in process #%d; ", tid, pid);
printf(" I'm about to spin untill main enters a non-zero value into the shared variable ");
while (value == 0){
}
printf("Second thread: Main set the shared variable to %d; ",value);
printf("I'll set it back to zero and then I'm done ");
value = 0;
}
int main()
{
pid = getpid();
printf("Main:PID is %d ", pid);
pthread_create(&tid, NULL, myThreadFun, NULL);
printf("Main:Successfully created the the thread with TID of %d ", tid);
printf(" Enter a positve number : ");
scanf("%d", &value);
while (value > 0){
}
printf("Main: Second thread has re-zeroed the shared variable, so I'm done ");
exit(0);
}
Explanation / Answer
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include<pthread.h>
#include<errno.h>
#include <stdlib.h>
int value;
int pid;
pthread_t tid;
void *myThreadFun(void *vargp)
{
printf("Second thread: My TID is %d, still in process #%d; ", tid, pid);
printf(" I'm about to spin untill main enters a non-zero value into the shared variable ");
while (value == 0){
}
printf("Second thread: Main set the shared variable to %d; ",value);
printf("I'll set it back to zero and then I'm done ");
value = 0;
}
int main()
{
pid = getpid();
printf("Main:PID is %d ", pid);
if (pthread_create(&tid, NULL, myThreadFun, NULL)){
printf("Error: %s ", strerror(errno));
}
printf("Main:Successfully created the the thread with TID of %d ", tid);
printf(" Enter a positve number : ");
scanf("%d", &value);
while (value > 0){
}
printf("Main: Second thread has re-zeroed the shared variable, so I'm done ");
exit(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.