Skip to content
Snippets Groups Projects
Commit 608dd5d9 authored by JieunYoon's avatar JieunYoon
Browse files

Add ledThread assignment

parent 6bc83905
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
void * thread_function(void *);
int Counter=0;
int main(void)
{
pthread_t tid1, tid2;
void *thread_result;
if(pthread_create(&tid1, NULL, thread_function, "thread1")!=0) {
perror("pthread_create");
exit(1);
}
if(pthread_create(&tid2, NULL, thread_function, "thread2")!=0) {
perror("pthread_create");
exit(1);
}
if(pthread_join(tid1, &thread_result)!=0) {
perror("pthread_join");
exit(1);
}
if(pthread_join(tid2, &thread_result)!=0) {
perror("pthread_join");
exit(1);
}
printf(" thread result : %s\n", (char *) thread_result);
return 0;
}
void * thread_function(void * arg)
{
int temp;
int i, j;
char buffer[80];
printf("thread_function called\n");
for(i=0; i<20; i++) {
sprintf(buffer, "%s : Counter : from %d to ", (char*)arg, Counter); //sprintf : 배열에 찍는것
write(1, buffer, strlen(buffer));
temp=Counter;
temp = temp+1;
// delay
for(j=0; j<500000; j++);
Counter=temp;
sprintf(buffer, " %d\n", Counter);
write(1, buffer, strlen(buffer));
}
pthread_exit("thread end");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
void * thread_function(void *);
int Counter=0;
#ifndef NOMUTEX
pthread_mutex_t mutex_id;
#endif
int main(void)
{
pthread_t tid1, tid2;
void *thread_result;
#ifndef NOMUTEX
if(pthread_mutex_init(&mutex_id, NULL) != 0){
perror("pthread_mutex_init");
exit(errno);
}
#endif
if(pthread_create(&tid1, NULL, thread_function, "thread1")!=0) {
perror("pthread_create");
exit(1);
}
if(pthread_create(&tid2, NULL, thread_function, "thread2")!=0) {
perror("pthread_create");
exit(1);
}
if(pthread_join(tid1, &thread_result)!=0) {
perror("pthread_join");
exit(1);
}
if(pthread_join(tid2, &thread_result)!=0) {
perror("pthread_join");
exit(1);
}
#ifndef NOMUTEX
pthread_mutex_lock(&mutex_id);
#endif
printf(" thread result : %s\n", (char *) thread_result);
return 0;
}
void * thread_function(void * arg)
{
int temp;
int i, j;
char buffer[80];
printf("thread_function called\n");
for(i=0; i<8; i++) {
#ifndef NOMUTEX
pthread_mutex_lock(&mutex_id);
#endif
sprintf(buffer, "%s: CountRelay: from %d to ", (char*)arg, Counter);
write(1, buffer, strlen(buffer));
temp=Counter;
temp = temp+1;
// delay
for(j=0; j<5000000; j++);
Counter=temp;
sprintf(buffer, "%d\n", Counter);
write(1, buffer, strlen(buffer));
#ifndef NOMUTEX
pthread_mutex_unlock(&mutex_id);
#endif
}
// getchar();
pthread_exit("thread end");
}
SRCS := led_main.c led_green.c led_yellow.c
OBJS := $(SRCS:.c=.o)
CC := gcc
#컴파일 옵션
CFLAGS = -Wall -g
#linking 옵션
LDFLAGS = -lpthread -lwiringPi
led_main : $(OBJS)
$(CC) -o $@ $^ ${LDFLAGS}
# dummy target
clean :
-rm $(OBJS)
-rm led_main
dep :
gccmakedep ${SRCS}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <pthread.h>
#define LED_RED 1
#define LED_GREEN 4
#define LED_YELLOW 5
void* led_yellow_task(void* args);
void* led_green_task(void* args);
\ No newline at end of file
#include "led.h"
void * led_green_task(void *arg)
{
int i;
pinMode(LED_GREEN, OUTPUT);
for(i=0; i<(int)arg; i++) {
digitalWrite(LED_GREEN, HIGH);
printf("GREEN ON!\n");
sleep(2);
digitalWrite(LED_GREEN, LOW);
printf("GREEN OFF!\n");
sleep(2);
}
pthread_exit(arg);
}
\ No newline at end of file
File added
#include "led.h"
void led_red_task()
{
pinMode(LED_RED, OUTPUT);
for(int i=0; i<10; i++) {
digitalWrite(LED_RED, HIGH);
printf("RED ON!\n");
sleep(1);
digitalWrite(LED_RED, LOW);
printf("RED OFF!\n");
sleep(1);
}
}
int main(int argc, char *argv[])
{
int gno;
int count;
pthread_t tid_Y, tid_G;
if(argc < 2) {
wiringPiSetup();
led_red_task();
}
else{
gno = atoi(argv[1]);
count = atoi(argv[2]);
wiringPiSetup();
if (gno == 1) {
led_red_task();
if (pthread_create(&tid_G, NULL, led_green_task, (void*)count) != 0)
{
perror("pthread_create");
exit(1);
}
} else if (gno == 2) {
led_red_task();
if (pthread_create(&tid_Y, NULL, led_yellow_task, (void*)count) != 0)
{
perror("pthread_create");
exit(1);
}
} else if(gno == 3){
led_red_task();
if (pthread_create(&tid_Y, NULL, led_yellow_task, (void*)count) != 0)
{
perror("pthread_create");
exit(1);
}
if (pthread_create(&tid_G, NULL, led_green_task, (void*)count) != 0)
{
perror("pthread_create");
exit(1);
}
}
}
pthread_exit(NULL);
return 0;
}
#include "led.h"
void * led_yellow_task(void *arg)
{
int i;
pinMode(LED_YELLOW, OUTPUT);
for(i=0; i<(int)arg; i++) {
digitalWrite(LED_YELLOW, HIGH);
printf("YELLOW ON!\n");
sleep(3);
digitalWrite(LED_YELLOW, LOW);
printf("YELLOW OFF!\n");
sleep(3);
}
pthread_exit(arg);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment