Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this code but it is not running or printing correctly. Please fix this code for me and post it here. the output that

I have this code but it is not running or printing correctly. Please fix this code for me and post it here.

the output that I am getting is:

Begin Multi-threading

RUN FINISHED; Segmentation fault: 11; real time: 0ms; user: 0ms; system: 0ms

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/* * File: main.c * * * Created on April 1, 2018, 5:00 PM */

#include #include

#include

// Using one global variable

int var = 0;

// This function is called whenever a new thread starts execution

void *runThread(void *vargp)

{

int *myid = (int *)vargp;

// Using one local variable along with global variable

static int varLocal = 0;

// Print the argument, static and global variables

printf("Thread ID: %d, Value of local variable: %d, Value of global variable: %d ", *myid, ++varLocal, ++var);

++varLocal;

++var;

}

int main(int argc, char** argv) { int i;

pthread_t tid;

printf("Begin Multi-threading ");

pthread_create(&tid, NULL, runThread, (void *)1);

pthread_create(&tid, NULL, runThread, (void *)2);

pthread_create(&tid, NULL, runThread, (void *)3);

pthread_create(&tid, NULL, runThread, (void *)4);

pthread_join(tid, NULL);

printf("All threads are done! ");

pthread_exit(NULL);

return 0;

return (EXIT_SUCCESS); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions