Skip to content

Instantly share code, notes, and snippets.

@AlessandroSpallina
Created February 24, 2015 14:58
Show Gist options
  • Save AlessandroSpallina/6d2ebb430be4a98b7bb9 to your computer and use it in GitHub Desktop.
Save AlessandroSpallina/6d2ebb430be4a98b7bb9 to your computer and use it in GitHub Desktop.
/*
Author: Alessandro Spallina
Website: http://aleksnote.altervista.org
Email: alessandrospallina1@gmail.com
* Creare un programma che:
- Chiede all'utente di inserire il proprio nome.
- Scrive a video "Ciao".
- Crea un thread che scrive a video "sono", aspetta
2 secondi ed esce.
- Aspetta la chiusura del thread, crea un altro
thread che scrive a video il nome dell'utente
richiesto prima ed esce.
- Il thread principale deve attendere anche la
terminazione di questo secondo thread.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
//************************************************************
void *primo(){
printf("\nsono ");
sleep(2);
pthread_exit(NULL);
}
void *secondo(void *arg){
printf("%s\n", arg);
pthread_exit(NULL);
}
//************************************************************
int main(){
char buf[152];
pthread_t attr;
int result;
printf("Nome: ");
scanf("%s", buf);
printf("\n\nCiao");
result=pthread_create(&attr, NULL, primo, NULL);
if(result==0){
pthread_join(attr, NULL);
result=pthread_create(&attr, NULL, secondo, (char *)buf);
if(result==0){
pthread_join(attr, NULL);
exit(EXIT_SUCCESS);
}
else{
perror("\n\nERR: Impossibile Creare Thread 2");
exit(EXIT_FAILURE);
}
}
else{
perror("\n\nERR: Impossibile Creare Thread 1");
exit(EXIT_FAILURE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment