Commit 009c3706 by PotatoGim

이전 예제들을 히스토리 없이 가져오기

parent 027cb3bc
CC = /usr/bin/gcc
RM = /bin/rm -f
LIBRARY = -L./lib -lpthread
INCLUDE = -I./inc
CFLAGS = -O2 -W
CFLAGS += $(LIBRARY) $(INCLUDE)
TARGET = alarm alarm_fork alarm_thread
.PHONY: all clean
all : ;
make $(TARGET) ; \
chmod 777 ./bin/*
clean : ;
$(RM) ./obj/* ./bin/*
alarm : alarm.o
gcc $(CFLAGS) -o ./bin/alarm ./obj/alarm.o
alarm.o : alarm.c
gcc $(CFLAGS) -c -o ./obj/alarm.o alarm.c
alarm_fork : alarm_fork.o
gcc $(CFLAGS) -o ./bin/alarm_fork ./obj/alarm_fork.o
alarm_fork.o : alarm_fork.c
gcc $(CFLAGS) -c -o ./obj/alarm_fork.o alarm_fork.c
alarm_thread : alarm_thread.o
gcc $(CFLAGS) -o ./bin/alarm_thread ./obj/alarm_thread.o
alarm_thread.o : alarm_thread.c
gcc $(CFLAGS) -c -o ./obj/alarm_thread.o alarm_thread.c
#include "errors.h"
int main (int argc, char *argv[])
{
int seconds;
char line[128];
char message[64];
while (1) {
printf ("Alaram> ");
if (fgets(line, sizeof(line), stdin) == NULL) exit (0);
if (strlen(line) <= 1) continue;
/*
* Parse input line into seconds (%d) and a message
* (%64[^\n]), consisting of up to 64 characters
* separated from the seconds by whitespace.
*/
if (sscanf (line, "%d %64[^\n]", &seconds, message) < 2) {
fprintf (stderr, "Bad command\n");
} else {
sleep (seconds);
printf ("(%d) %s\n", seconds, message);
}
}
}
#include <sys/types.h>
#include <sys/wait.h>
#include "errors.h"
int main (int argc, char *argv[])
{
int status;
char line [128];
int seconds;
pid_t pid;
char message[64];
while (1) {
printf ("Alarm> ");
if (fgets (line, sizeof(line), stdin) == NULL) exit (0);
if (strlen (line) <= 1) continue;
/*
* Parse input line into seconds (%d) and a message
* (%64[^\n]), consisting of up to 64 characters
* separated from the seconds by whitespace.
*/
if (sscanf (line, "%d %64[^\n]", &seconds, message) < 2) {
fprintf (stderr, "Bad command\n");
} else {
pid = fork();
if (pid == (pid_t) -1)
errno_abort ("Fork");
if (pid == (pid_t) 0) {
/*
* In the child, wait and then print a message
*/
sleep (seconds);
printf ("(%d) %s\n", seconds, message);
exit (0);
} else {
/*
* In the parent, call waitpid() to collect children
* that have already terminated.
*/
do {
pid = waitpid ((pid_t) -1, NULL, WNOHANG);
if (pid == (pid_t) -1)
errno_abort ("Wait for child");
} while (pid != (pid_t) 0);
}
}
}
}
#include <pthread.h>
#include "errors.h"
typedef struct alarm_tag {
int seconds;
char message[64];
} alarm_t;
void *alarm_thread (void *arg)
{
alarm_t *alarm = (alarm_t *) arg;
int status;
status = pthread_detach (pthread_self());
if (status != 0)
err_abort (status, "Detach thread");
sleep (alarm->seconds);
printf ("(%d) %s\n", alarm->seconds, alarm->message);
free (alarm);
return NULL;
}
int main (int argc, char *argv[])
{
int status;
char line[128];
alarm_t *alarm;
pthread_t thread;
while (1) {
printf ("Alarm> ");
if (fgets(line, sizeof(line), stdin) == NULL) exit (0);
if (strlen(line) <= 1) continue;
alarm = (alarm_t *) malloc (sizeof(alarm_t));
if (alarm == NULL)
errno_abort ("Allocate alarm");
/*
* Parse input line into seconds (%d) and a message
* (%64[^\n]), consisting of up to 64 characters
* separated from the seconds by whitespace.
*/
if (sscanf(line, "%d %64[^\n]", &alarm->seconds, alarm->message) < 2) {
fprintf (stderr, "Bad command\n");
free (alarm);
} else {
status = pthread_create (&thread, NULL, alarm_thread, alarm);
if (status != 0)
err_abort (status, "Create alarm thread");
}
}
}
#ifndef __errors_h
#define __errors_h
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Define a macro that can be used for diagnostic output from
* examples. When compiled -DDEBUG, it results in calling printf
* with the specified argument list. When DEBUG is not defined, it
* expands to nothing.
*/
#ifdef DEBUG
# define DPRINTF(arg) printf arg
#else
# define DPRINTF(arg)
#endif
/*
* NOTE: the "do {" ... "} while (0);" bracketing around the macros
* allows the err_abort and errno_abort macros to be used as if they
* were function calls, even in contexts where a trailing ";" would
* generate a null statement. For example,
*
* if (status != 0)
* err_abort (status, "message");
* else
* return status;
*
* will not compile if err_abort is a macro ending with "}", because
* C does not expect a ";" to follow the "}". Because C does expect
* a ";" following the ")" in the do...while construct, err_abort and
* errno_abort can be used as if they were function calls.
*/
#define err_abort(code,text) do { \
fprintf (stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror (code)); \
abort (); \
} while (0)
#define errno_abort(text) do { \
fprintf (stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror (errno)); \
abort (); \
} while (0)
#endif
CC = /usr/bin/gcc
RM = /bin/rm -f
LIBRARY = -lpthread
INCLUDE = -I./inc
CFLAGS = -O2 -W
CFLAGS += $(LIBRARY) $(INCLUDE)
TARGET = lifecycle
.PHONY: all clean
all : ;
make $(TARGET)
clean : ;
$(RM) ./obj/* ./bin/*
lifecycle : lifecycle.o
gcc $(CFLAGS) -o ./bin/$@ ./obj/$^ ; \
chmod 777 ./bin/$@
lifecycle.o : lifecycle.c
gcc $(CFLAGS) -c -o ./obj/$@ $^
#ifndef __errors_h
#define __errors_h
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Define a macro that can be used for diagnostic output from
* examples. When compiled -DDEBUG, it results in calling printf
* with the specified argument list. When DEBUG is not defined, it
* expands to nothing.
*/
#ifdef DEBUG
# define DPRINTF(arg) printf arg
#else
# define DPRINTF(arg)
#endif
/*
* NOTE: the "do {" ... "} while (0);" bracketing around the macros
* allows the err_abort and errno_abort macros to be used as if they
* were function calls, even in contexts where a trailing ";" would
* generate a null statement. For example,
*
* if (status != 0)
* err_abort (status, "message");
* else
* return status;
*
* will not compile if err_abort is a macro ending with "}", because
* C does not expect a ";" to follow the "}". Because C does expect
* a ";" following the ")" in the do...while construct, err_abort and
* errno_abort can be used as if they were function calls.
*/
#define err_abort(code,text) do { \
fprintf (stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror (code)); \
abort (); \
} while (0)
#define errno_abort(text) do { \
fprintf (stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror (errno)); \
abort (); \
} while (0)
#endif
#include <pthread.h>
#include "errors.h"
/*
* Thread start routine.
*/
void *thread_routine (void *arg)
{
return arg;
}
main (int argc, char *argv[])
{
pthread_t thread_id;
void *thread_result;
int status;
status = pthread_create (&thread_id, NULL, thread_routine, NULL);
if (status != 0)
err_abort (status, "Create thread");
status = pthread_join (thread_id, &thread_result);
if (status != 0)
err_abort (status, "Join thread");
if (thread_result == NULL)
return 0;
else
return 1;
}
CC = /usr/bin/gcc
RM = rm -f
LIBRARY = -lpthread
INCLUDE = -I./inc
CFLAGS = -O2 -W
CFLAGS += $(INCLUDE) $(LIBRARY)
TARGET = mutex_static mutex_dynamic alarm_mutex trylock backoff cond_static cond_dynamic cond alarm_cond
.PHONY : all clean
all : ;
make $(TARGET)
clean : ;
$(RM) ./obj/* ./bin/*
mutex_static.o : mutex_static.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
mutex_static : mutex_static.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
mutex_dynamic.o : mutex_dynamic.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
mutex_dynamic : mutex_dynamic.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
alarm_mutex.o : alarm_mutex.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
alarm_mutex : alarm_mutex.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
trylock.o : trylock.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
trylock : trylock.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
backoff.o : backoff.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
backoff : backoff.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
cond_static.o : cond_static.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
cond_static : cond_static.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
cond_dynamic.o : cond_dynamic.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
cond_dynamic : cond_dynamic.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
cond.o : cond.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
cond : cond.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
alarm_cond.o : alarm_cond.c
$(CC) $(CFLAGS) -c -o ./obj/$@ $<
alarm_cond : alarm_cond.o
$(CC) $(CFLAGS) -o ./bin/$@ ./obj/$< ; \
chmod 777 ./bin/$@
#include <pthread.h>
#include <time.h>
#include "errors.h"
/*
* The "alarm" structure now contains the time_t (time since the
* Epoch, in seconds) for each alarm, so that they can be
* sorted. Storing the requested number of seconds would not be
* enough, since the "alarm thread" cannot tell how long it has
* been on the list.
*/
typedef struct alarm_tag {
struct alarm_tag *link;
int seconds;
time_t time; /* seconds from EPOCH */
char message[64];
} alarm_t;
pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t alarm_cond = PTHREAD_COND_INITIALIZER;
alarm_t *alarm_list = NULL;
time_t current_alarm = 0;
/*
* Insert alarm entry on list, in order.
*/
void alarm_insert (alarm_t *alarm)
{
int status;
alarm_t **last, *next;
/*
* LOCKING PROTOCOL:<