I have this assignment for Operating Systems, the code won't run in MAC terminal, I keep getting an issue that says, "Image". Is there any help I can get on this issue?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// These two functions will run concurrently
void* print_i(void *ptr)
{
printf("I am in i\n");
}
void* print_j(void *ptr)
{
printf("I am in j\n");
}
int main()
{
pthread_t t1,t2;
int rc1 = pthread_create(&t1, NULL, print_i, NULL);
int rc2 = pthread_create(&t2, NULL, print_j, NULL);
exit(0);
}
The keyword void (not a pointer) means "nothing" in C. This is consistent.
As you noted, void* means "pointer to anything" in languages that support raw pointers (C and C++). This means you need to return something.
This is the reason why you get this type of warning.
If you return someting, like return 0; warnings disapear.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// These two functions will run concurrently
void* print_i(void *ptr)
{
printf("I am in i\n");
return 0;
}
void* print_j(void *ptr)
{
printf("I am in j\n");
return 0;
}
int main()
{
pthread_t t1,t2;
int rc1 = pthread_create(&t1, NULL, print_i, NULL);
int rc2 = pthread_create(&t2, NULL, print_j, NULL);
exit(0);
}
You can learn more about it here:
https://linux.die.net/man/3/pthread_create
Related
I've been trying to create a chat program in C with threads but it hasn't been working, so I decided to play with threads for a bit first. I'm trying to run a thread that prints "hello world", but it gives me a segmentation fault. I haven't been able to find the root of the problem so I came here. heres the code:
#include <stdio.h>
#include <pthread.h>
void* test(void * arg) {
printf("hello world\n");
return NULL;
}
int main() {
pthread_t test;
pthread_create(&test, NULL, (void *) test, NULL);
pthread_exit(NULL);
return 0;
}
There's probably a stupid reason why it's not working so I hope you guys wont have too much trouble finding it!
Your start routine name and thread id name are the same so I think compiler gets confused when you pass &test. Your code works by changing the thread id name.
#include <stdio.h>
#include <pthread.h>
void* test(void * arg) {
printf("hello world\n");
return NULL;
}
int main() {
pthread_t t;
pthread_create(&t, NULL, (void *) test, NULL);
pthread_exit(NULL);
return 0;
}
void* test(void * arg) { ... }
// vvvv
pthread_t test;
pthread_create(&test, NULL, (void *) test, NULL);
// ^^^^
That pthread_t variable is "hiding" the function name. In other words, you're calling some arbitrary uninitialised value as your function. That's unlikely to end well :-)
What you're doing is really no different to expecting the following program to output 7 (it won't):
#include <stdio.h>
int i = 7;
int main(void) {
int i = 42;
printf("%d\n", i);
return 0;
}
You could solve it simply by renaming your function to testFn, for example.
It gets me error at rand_r
undefined reference to 'rand_r'
It is my first program at c language
I must use rand_r to threads, but i don't know what to do.
Please help me. Thank you
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "p3160062.h"
int cook;
int oven;
int prep;
int bake;
pthread_mutex_t lock;
pthread_t thread_id[100];
unsigned int seed;
int a;
void *myThread(void *vargp)
{
pthread_mutex_lock(&lock);
a=rand_r(&seed)%6;
while(a==0){
a=rand_r(&seed)%6;
}
printf("a=%d\n",a);
sleep(1);
printf("In thread\n");
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
seed=1;
int i=0;
printf("Before Thread\n");
for(i=0;i<100;i++){
pthread_create(&(thread_id[i]), NULL, myThread, NULL);
pthread_join(thread_id[i], NULL);
seed++;
}
printf("After Thread\n");
pthread_mutex_destroy(&lock);
exit(0);
}
I've use pthread for multithreded program and I've got the following situation.
When I run the code without sleep command it causes error at run time, and when I add the sleep command program runs as expected.
With sleep:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;
void *print_str(void *args) {
sleep(12);
char *str = (char*) args;
pthread_mutex_lock(&m_writer);
printf("%s", str);
pthread_mutex_unlock(&m_writer);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t t1;
pthread_create(&t1, NULL, print_str, "Hello\n");
pthread_mutex_lock(&m_writer);
printf("LOL\n");
pthread_mutex_unlock(&m_writer);
pthread_join(t1, NULL);
return 0;
}
Without sleep:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;
void *print_str(void *args) {
char *str = (char*) args;
pthread_mutex_lock(&m_writer);
printf("%s", str);
pthread_mutex_unlock(&m_writer);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t t1;
pthread_create(&t1, NULL, print_str, "Hello\n");
pthread_mutex_lock(&m_writer);
printf("LOL\n");
pthread_mutex_unlock(&m_writer);
pthread_join(t1, NULL);
return 0;
}
Error:
futex(0x559c3d3df0a0, FUTEX_WAIT_PRIVATE, 2, NULLHello
) = -1 EAGAIN (Resource temporarily unavailable)
strace shows results of system calls. Pthread functions in Linux are not system calls, they are libc functions implemented on top of (potentilally non-trivial) system calls. You are not interested in what internal system calls return. A single successfull call to ptread_mutex_lock may and sometimes does entail several failing syscalls.
In particular, pthread_mutex_lock cannot possibly result in EAGAIN in this program because there are no attempts to lock the mutex recursively, and the default mutex is not recursive in Linux anyway. The FUTEX_WAIT_PRIVATE syscall that pthread_mutex_lock uses internally can and will result in EAGAIN. This is of no interest whatsoever to the application programmer.
I am using ucontext along with pthread. The below program works OK on Linux, but has failed assertion on Mac.
The problem seems that thread local variables are not correctly accessed after resuming the context from another thread.
The program creates two threads, A and B. A sets the context ready before B could resume the context, for it's synced properly.
It's very appreciated if someone could shed some light on this behavior on mac.
ENV:
clang version 3.7.0 (http://llvm.org/git/llvm.git 8d70064a4ac2ae09b8003173e751cfad9dc15400)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Program:
#define _XOPEN_SOURCE 800
#include <ucontext.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
static int flag = 0;
void swap(ucontext_t *old, ucontext_t *new)
{
int ret = swapcontext(old, new);
assert(ret == 0);
}
#define SSIZE MINSIGSTKSZ
static char stack[SSIZE];
static ucontext_t a_ctx[2];
static ucontext_t b_ctx[2];
volatile static __thread int bug = 0;
static void func(int b) { }
static void f1 (void)
{
assert(bug == 0);
func(bug);
swap(&a_ctx[1], &a_ctx[0]);
assert(bug == 1);
}
void *thread_a(void *arg)
{
printf("A is %lu\n", pthread_self());
bug = 0;
ucontext_t ctx = a_ctx[1];
getcontext(&ctx);
ctx.uc_stack.ss_sp = stack;
ctx.uc_stack.ss_size = sizeof stack;
makecontext(&ctx, f1, 0);
swap(&a_ctx[0], &ctx);
__atomic_store_n(&flag, 1, __ATOMIC_RELAXED);
sleep(1);
return NULL;
}
void *thread_b(void *arg)
{
printf("B is %lu\n", pthread_self());
bug = 1;
while(__atomic_load_n(&flag, __ATOMIC_RELAXED) == 0) ;
swap(&b_ctx[0], &a_ctx[1]);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t a, b;
pthread_create(&b, NULL, &thread_b, NULL);
pthread_create(&a, NULL, &thread_a, NULL);
pthread_exit(NULL);
}
This is my code, I compiled it with:
gcc thread.c -lpthread
It didn't print any error or warning. But when I run it, the program doesn't print anything.
Platform: Ubuntu 11.10 64 bit gcc 4.6.1
Exit status :0
When I debug it, I found it prints hello as I expected.
This is my code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *loopPrint(void *ptr)
{
char *p = (char *)ptr;
while (1)
{
printf("%s\n", p);
}
}
void *pClock(void *ptr)
{
sleep(3);
exit(0);
}
int main()
{
pthread_t showMsg, clock;
int main_pth, wait_pth;
char *msg = "Hello";
main_pth = pthread_create(&showMsg, NULL, loopPrint, (void *)msg);
wait_pth = pthread_create(&clock, NULL, pClock, NULL);
pthread_join(main_pth, NULL);
pthread_join(wait_pth, NULL);
return 0;
}
pthread_join(main_pth, NULL);
This is wrong, pthread_join takes a pthread_t as an argument. Replace with:
pthread_join(showMsg, NULL);
(Same for the other one.)
And do check the return values of these calls, they can and do fail.
And BTW, you're missing #include <unistd.h> for the sleep call.
main_pth etc is the error return of pthread_create and not the thread id. Wait ("join") for showMsg and clock.