This is the snippet of the code. I am able to set the name of thread. However, I get an error while retrieving the name of the thread. Please help.
void *Thread_Function_A(void *thread_arg)
{
char buf[7];
int rc;
pthread_t self;
self = pthread_self ();
rc = pthread_getname_np(self, buf,7);
if ( rc != 0 )
cout<<"Failed getting the name"<<endl;
}
int main(int argc, char *argv[])
{
int rc;
pid_t thread_pid_val = getpid();
thread_1.create_thread((thread_1.get_thread_id()), NULL,Thread_Function_A,&thread_pid_val);
thread_2.create_thread((thread_2.get_thread_id()), NULL,Thread_Function_A,&thread_pid_val);
rc = pthread_setname_np(*(thread_1.get_thread_id()), "Thread_A");
if( rc != 0)
{
cout<<"Setting name for thread A failed"<<endl;
}
rc = pthread_setname_np(*(thread_2.get_thread_id()), "Thread_B");
if( rc != 0)
{
cout<<"Setting name for thread B failed"<<endl;
}
pthread_join( *(thread_1.get_thread_id()), NULL);
pthread_join( *(thread_2.get_thread_id()), NULL);
return 0;
}
output : -
$./thread_basic.out
Failed getting the nameFailed getting the name
The name of thread is The name of thread is
The strerror says - Numerical result out of range
error =34
Added now complete code. Here, I don't get the right name set. Instead, it retrieves the name of the program.
void *Thread_Function_A(void *thread_arg)
{
char name[300];
char buf[200];
int rc;
char message[100];
FILE *fp;
pthread_t self;
self = pthread_self ();
rc = pthread_getname_np(self, buf,200);
if ( rc != 0 )
{
cout<<"Failed getting the name"<<endl;
cerr<<"Pthread get name error ="<<rc<< " " << strerror(rc) << endl;
}
sprintf(name,"log_%s.txt",buf);
cout<<"The name of thread is "<<buf<<endl;
fp = fopen(name,"w+");
for( int i = 1; i<=5; i++)
{
sprintf(message,"The thread id is %d and value of i is %d",pthread_self(),i);
fprintf(fp,"%s\n", message);
fflush(fp);
/** local variable will not be shared actually**/
/** each thread should execute the loop for 5 **/
/** total prints should be 10 **/
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int rc;
pthread_t threadA, threadB;
pid_t thread_pid_val = getpid();
thread_1.create_thread(&threadA, NULL,Thread_Function_A,&thread_pid_val);
thread_1.set_thread_id(threadA);
rc = pthread_setname_np(threadA, "Thread_A");
if( rc != 0)
{
cout<<"Setting name for thread A failed"<<endl;
}
thread_2.create_thread(&threadB, NULL,Thread_Function_A,&thread_pid_val);
thread_2.set_thread_id(threadB);
rc = pthread_setname_np(threadB, "Thread_B");
if( rc != 0)
{
cout<<"Setting name for thread B failed"<<endl;
}
pthread_join( threadA, NULL);
pthread_join( threadB, NULL);
return 0;
}
The output is the following.
]$ ./thread_basic.out
The name of thread is thread_basic.ou
The name of thread is Thread_B
In addition to the race condition, which won't make your call fail but may not return what you want, this is why the call fails.
man 3 pthread_getname_np
The pthread_getname_np() function can be used to retrieve the name of the thread. The thread argument specifies the thread whose name is to be retrieved. The buffer name is used to return the thread name; len specifies the number of bytes available in name. The buffer specified by name should be at least 16 characters in length. The returned thread name in the output buffer will be null terminated.
char buf[7];
is going to fail.
You are using a mix of C and C++ features which you should avoid, and with parts of code that we can't verify, e.g your method get_thread_id. But the problem of your code is obvious, you are trying to obtain the the names of the threads far too early: your main has not the slightest chance to place the names before the threads are already terminated.
Also, your thread function is not correct, and any compiler with a minimum level of warning should have told you. A function with a non-void return type needs a return statement. In C this would only lead to undefined behavior if you'd use the return value of the function, which you can't know since it is the thread library which calls your function.
Related
void* camera(void* arg)
{
int id = *(int *)arg;
cout << "please";
sleep(interval); // sleep interval seconds
int done = 0; /* 0 - not done; 1 - done */
long mysell = 0;
int s;
while(frame_cnt < 10){
if (rear == n - 1){// check if camera cache is full
sleep(interval);
}
else{
generate_frame_vector(8);
for (int i = 0; i < 8; i++){
rear++;
queue[rear] = frame_vector[i];
}
}
if(frame_cnt >= 10){
pthread_exit((void *) mysell);
}
}
}
int main(int argc, char* argv[]) { // Start
// Enter command, e.g: ./51234567 2 to run this program
if(argc == 2){ // if input is legal continue below with the rest of program execution
interval = atoi(argv[1]); // int interval stores the int value that will be used in sleep() later on
pthread_t threads;
int threadedid;
int rc; // rc is used to get the return value of pthread functions
rc = pthread_create(&threads, NULL, camera, (void *)&threadedid);
if (rc){
cout << "Error occured when creating the camera thread" << endl;
exit(-1);
}
}
else { // if input is not legal, then program will not fail to run and the message below displayed to the user
cerr << "Error occured because your argument is wrong; please use the format: './51234567 2' where 2 is the interval you want to use"
<< endl;
}
return 0;
}
Your program terminates immediately after the thread has been created because you don't wait for the thread to finish.
Add this somewhere before main() returns:
void* retval;
int r = pthread_join(threads, &retval);
if(r==0) // successfully joined the thread threads
else // failed to join the thread threads
I need some algorithm help with a multithreaded program I'm writing. It's basically the cp command in unix, but with a read thread and a write thread. I'm using semaphores for thread synchronization. I have structs for buffer and thread data defined as
struct bufType {
char buf[BUFFER_SIZE];
int numBytes;
};
struct threadData {
int fd;
bufType buf;
};
and a global array of bufType. Code for my main is
int main(int argc, const char * argv[])
{
int in, out;
pthread_t Producer, Consumer;
threadData producerData, consumerData;
if (argc != 3)
{
cout << "Error: incorrect number of params" << endl;
exit(0);
}
if ((in = open(argv[1], O_RDONLY, 0666)) == -1)
{
cout << "Error: cannot open input file" << endl;
exit(0);
}
if ((out = open(argv[2], O_WRONLY | O_CREAT, 0666)) == -1)
{
cout << "Cannot create output file" << endl;
exit(0);
}
sem_init(&sem_empty, 0, NUM_BUFFERS);
sem_init(&sem_full, 0, 0);
pthread_create (&Producer, NULL, read_thread, (void *) &producerData);
pthread_create (&Consumer, NULL, write_thread, (void *) &consumerData);
pthread_join(Producer, NULL);
pthread_join(Consumer, NULL);
return 0;
}
and read and write threads:
void *read_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
while((thread_data->buf.numBytes = slow_read(thread_data->fd, thread_data->buf.buf, BUFFER_SIZE)) != 0)
{
sem_post(&sem_full);
sem_wait(&sem_empty);
}
pthread_exit(0);
}
void *write_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
sem_wait(&sem_full);
slow_write(thread_data->fd, thread_data->buf.buf, thread_data->buf.numBytes);
sem_post(&sem_empty);
pthread_exit(0);
}
So my issue is in what to assign to my threadData variables in main, and my semaphore logic in the read and write threads. I appreciate any help you're able to give
Being a windows guy who does not use file descriptors I might be wrong with the in's and out's but I think this needs to be done in your main in order to setup the threadData structures.
producerData.fd = in;
consumerData.fd = out;
Then declare ONE SINGLE object of type bufType for both structures. Change for example the definition of threadData to
struct threadData {
int fd;
bufType* buf;
};
and in your Main, you write
bufType buffer;
producerData.buf = &buffer;
consumerData.buf = &buffer;
Then both threads will use a common buffer. Otherwise you would be writing to the producerData buffer, but the consumerData buffer will stay empty (and this is where your writer thread is looking for data)
Then you need to change your signalling logic. Right now your program cannot accept input that exceeds BUFFER_SIZE, because your write thread will only write once. There needs to be a loop around it. And then you need some mechanism that signals the writer thread that no more data will be sent. For example you could do this
void *read_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
while((thread_data->buf->numBytes = slow_read(thread_data->fd, thread_data->buf->buf, BUFFER_SIZE)) > 0)
{
sem_post(&sem_full);
sem_wait(&sem_empty);
}
sem_post(&sem_full); // Note that thread_data->buf->numBytes <= 0 now
pthread_exit(0);
}
void *write_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
sem_wait(&sem_full);
while (thread_data->buf->numBytes > 0)
{
slow_write(thread_data->fd, thread_data->buf->buf, thread_data->buf->numBytes);
sem_post(&sem_empty);
sem_wait(&sem_full);
}
pthread_exit(0);
}
Hope there are no more errors, did not test solution. But the concept should be what you were asking for.
You could use a common buffer pool, either a circular array or a linked lists. Here is a link to a zip of a Windows example that is similar to what you're asking, using linked lists as part of a inter-thread messaging system to buffer data. Other than the creation of the mutexes, semaphores, and the write thread, the functions are small and simple. mtcopy.zip .
I am trying to dynamically create pthread and facing the issue in addressing of the variable. Can you please tell how the address should be accessed
int main (int argc, char *argv[])
{
pthread_t *threads;
int rc, numberOfThreads;
long t;
cout<<"Number of Threads = ";
cin>>numberOfThreads;
cout<<endl;
threads =(pthread_t*) malloc(numberOfThreads*sizeof(pthread_t));
for(t=0; t<numberOfThreads; t++){
printf("In main: creating thread %ld\n", t);
// **ERROR ON BELOW LINE**
rc = pthread_create((pthread_t)&(threads+numberOfThreads), NULL, FunctionForThread, (void *)t);
(void) pthread_join(threads[t], NULL);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
ERROR : lvalue required as unary ‘&’ operand
pthread_create() requires a pthread_t* type as the first parameter. You have an array of pthread_t so pass an address of one of them:
rc = pthread_create(&threads[i], NULL, FunctionForThread, (void *)t);
Also note that the cast (void *)t is not correct. You should pass a pointer to a valid object.
I have almost completed an homework assignment where I am required to use pthreads. I have figured out the pthreads. The only problem I have left is figuring out how to pass multiple arguments to threads through pthread_create().
I need to pass two chars to the thread. I have to cast them to (*void) to use with pthread_create(). I can pass them, but I can't figure out how to get the values from *parameter in the function.
void *my_function(void *parameter) {
/* ATTEMPT 1 - DOESN'T WORK */
//char* arguments[2];
//arguments = (char * [2]) parameter;
/*Error message:
error: ISO C++ forbids casting to an array type char* [2] [-fpermissive]
error: incompatible types in assignment of char** to char*[2]
*/
/* ATTEMPT 2 - DOESN'T WORK */
//char *my_data = (char *)parameter;
//my_data is blank when I try to use cout to check it's values
/* What I need to do is get those two chars from the array and print them out as part of this thread function */
pthread_exit(NULL);
}
int main(int argc, char **argv) {
char duration = '5'; //in reality, this value is taken from argv but I am leaving that out for brevity
pthread_t threads[3];
for(int i=0; i < 3; i++){
char thread_args[2] = {i, duration};
//create thread with arguments passed in
int results = pthread_create(&threads[i], NULL, my_function, (void *) &thread_args);
//testing for pthread error
if (results){
printf("ERROR; return code from pthread_create() is %d\n", results);
exit(-1);
}
}
/* Wait for all threads to complete */
for (int j=0; j < num_threads; j++) { // https://computing.llnl.gov/tutorials/pthreads/
pthread_join(threads[j], NULL);
}
/* some information prints here that is unrelated to my problem (the date, time, etc) */
pthread_exit(NULL);
}
I was able to pass one value through with no problem. Any suggestions?
The closest existing question I could find was this but I still am having no luck: Converting from void* to char ** in C
Thank you!
Note that within this loop:
for(int i=0; i < 3; i++){
char thread_args[2] = {i, duration};
int results = pthread_create(&threads[i], NULL, my_function, (void *)
...
}
thread_args is a local array with automatic storage duration, lifetime of which is tied to each iteration so there is a chance that the memory where you store these arguments might be freed before the thread will access it, which would lead to undefined behavior in that case.
Much better approach would be to create a structure, that you would use to pass the data to this thread:
typedef struct {
char duration;
int num;
} ThreadData;
Then your code could look like this:
void *my_function(void *parameter) {
// retrieve and print the thread data:
ThreadData* td = (ThreadData*) parameter;
printf("num = %d, duration = %c\n", td->num, td->duration);
delete td;
return NULL;
}
int main(int argc, char **argv) {
char duration = '5';
pthread_t threads[3];
for (int i = 0; i < 3; i++) {
// create structure that will be passed to thread:
ThreadData* td = new ThreadData;
td->duration = duration;
td->num = i;
//create thread with arguments passed in:
int ret = pthread_create(&threads[i], NULL, my_function, (void *) td);
//testing for pthread error:
if (ret) {
printf("ERROR; return code from pthread_create() is %d\n", ret);
exit(-1);
}
}
// wait for all threads to complete:
for (int i = 0; i < 3; i++) {
pthread_join(threads[i], NULL);
}
exit(0);
}
Also note that to end thread's execution it is better to use return than pthread_exit since with return it is guaranteed that variables within thread's routine will be destroyed and stack will be unwound. For more information see return() versus pthread_exit() in pthread start functions
I want to make it so when a user attaches a - after a command it will be executed in the background. For some reason if I execute a command normally it will wait, then if I execute a command in the background it will work but then if I execute a command normally it won't wait for it. I am sure I am just doing something small-ish wrong. Any ideas:
void executeSystemCommand(char *strippedCommand, char *background, int argc, char **args) {
char pathToExecute[80];
// Check if command will be executed in the background
int shellArgs;
bool bg;
if (!strcmp(background, "-")) {
bg = true;
shellArgs = argc -1;
} else {
bg = false;
shellArgs = argc;
}
// Save the linux commands in a new array
char *executableCommands[shellArgs+1];
int j;
for (j = 0; j < shellArgs+1; j++) {
executableCommands[j] = args[j];
}
executableCommands[shellArgs] = NULL;
// Check the $PATH
const char delimiters[] = ":";
char *token, *cp;
char *forLater;
int count = 0;
char *path;
path = getenv("PATH");
// All of this just breaks up the path into separate strings
cp = strdup(path);
forLater = strdup(path);
token = strtok (cp, delimiters);
while ((token = strtok (NULL, delimiters)) != NULL) {
count++;
}
char **argv;
int size = count+1;
argv = (char**) malloc (size);
count = 0;
token = strtok (forLater, delimiters);
argv[0] = (char*) malloc (50);
argv[0] = token;
strcpy(argv[0],token);
while ((token = strtok (NULL, delimiters)) != NULL) {
count++;
argv[count] = (char*) malloc (50);
argv[count] = token;
}
// This goes through the path to see if the linux command they entered
// Ex: sleep exists in one of those files and saves it to a var
int i;
bool weHaveIt = false;
int ac;
for (i = 0; i < count; i++) {
char str[80];
strcpy(str, argv[i]);
strcat(str, "/");
strcat(str, args[0]);
ac = access(str, F_OK);
if (ac == 0) {
weHaveIt = true;
strcpy(pathToExecute, str);
break;
}
}
if (!weHaveIt) {
printf("That is not a valid command. SORRY!\n");
return;
}
executableCommands[0] = pathToExecute;
int status;
// Get the array for
// If user wants command to be a background process
if (bg) {
int background_process_id;
pid_t fork_return;
fork_return = fork();
if (fork_return == 0) {
background_process_id = getpid();
addJobToTable(strippedCommand, background_process_id);
setpgid(0, 0);
execve(executableCommands[0], executableCommands, NULL);
exit(0);
} else {
return;
}
} else {
int background_process_id;
pid_t fork_return;
fork_return = fork();
if (fork_return == 0) {
background_process_id = getpid();
status = execve(executableCommands[0], executableCommands, NULL);
exit(0);
} else {
wait(&status);
return;
}
}
}
The call to wait made for the third job returns immediately because the second job has finished and is waiting to be handled (also called "zombie"). You could check the return value of wait(&status), which is the PID of the process that has exited, and make sure it is the process you were waiting for. If it's not, just call wait again.
Alternatively use waitpid, which waits for a specific process:
/* Wait for child. was: wait(&status) */
waitpid(fork_return, &status, 0);
If you do this you should implement a signal handler for SIGCHLD to handle finished background jobs to prevent the accumulation of "zombie" child processes.
In addition to that, in the background job case, the branch where fork() returns 0 you are already in the new process, so the call to addJobToTable happens in the wrong process. Also, you should check the return values of all the calls; otherwise something may be failing and you don't know it. So the code for running a job in the background should look more like this:
if (fork_return == 0) {
setpgid(0, 0);
if (execve(executableCommands[0], executableCommands, NULL) == -1) {
perror("execve");
exit(1);
}
} else if (fork_return != -1) {
addJobToTable(strippedCommand, fork_return);
return;
} else {
perror("fork"); /* fork failed */
return;
}
Every child process created with fork() will exit when the parent process exits.
if (fork_return == 0) {
/* child process, do stuff */
} else {
/* parent process, exit immediately */
return;
}
Explanation
fork spawns a new process as a child process of the current process (parent). Whenever a process in Unix-like operating systems terminates all of its child processes are going to be terminated too. If they have child processes on their own, then these will get terminated too.
Solution
On most shells you can start a process in background if you add an ampersand & to the end of the line:
myApplication arg1 arg2 arg3 ... argN &