Following code is a POSIX semaphore usage example. Fill in the blanks.

If you have any doubts, please give me comment...
int main(int argc, char** argv){
sem_t sem;
if(sem_init(&sem, 0, 1)==-1) printf("%s\n", strerror(errno));
if(sem_wait(&sem)!=0) printf("%s\n", strerror(errno));
printf("*** Critical Section ***\n");
if(sem_post(&sem)!=0) printf("%s\n", strerror(errno));
printf("*** Non-Critical Section ***\n");
if(sem_destroy(&sem)!=0) printf("%s\n", strerror(errno));
return 0;
}
Following code is a POSIX semaphore usage example. Fill in the blanks. (assume proper headers were...
Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By walking through this example you’ll learn: * - How to use pthread_create(). * - How to use pthread_exit(). * - How to use pthread_join(). * */ #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <sys/wait.h> void* ninja(void* arg){ printf("Who’s there?"); fflush(stdout); pthread_exit("ninja"); } // // Expected output: // // Knock knock. // Who's there? - from ninja // Knuc...kles. // int main(int argc, char* argv[]){...
#include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage: * ./wc <words | lines> <file> * * If argv[1] is "words", then you should count the number of words. If it * is "lines", then you should count the number of lines. * * For example: * $ cat a.txt * a b c d * $ ./wc words a.txt * 4 * $ ./wc lines a.txt * 1 * * YOUR PROGRAM...
Please Modify the following program, paste your code and show the execution result capture /** * Basic execl() Usage. * * By walking through this example you’ll learn: * - How to use execl(). * - What happens to the process that invoked execl(). * */ #include #include int main(int argc, char* argv[]){ printf("%s executing `ls -l`.\n", "Before"); // HINT: The /bin/ls -l should be executed. execl(); printf("%s executing `ls -l`.\n", "After"); return 0; }
If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...
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...
Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c=0,x=0; void *UpdateC1(void *arg) { int i; for(i=0;i<2000000;i++) { sem_wait(&s1); c++; x++; sem_post(&s1); } } void *UpdateC2(void *arg) { int i,x=0; for(i=0;i<2999999;i++) { sem_wait(&s1); c++; x++; ...
devmem2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s
(%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); }
while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc, char **argv) {
int fd;
void *map_base = NULL, *virt_addr = NULL;
unsigned long read_result, writeval;
off_t target;
int access_type = 'w';
if(argc...
Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...
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; //...