-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit from existing text base.
- Loading branch information
Peter Chapin
authored and
Peter Chapin
committed
Apr 5, 2016
1 parent
3392412
commit 1e5a9fd
Showing
26 changed files
with
3,379 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# tutorialpthread | ||
A pthread tutorial | ||
TutorialPthread | ||
=============== | ||
|
||
A pthreads tutorial. | ||
|
||
Peter C. Chapin | ||
[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/**************************************************************************** | ||
FILE : attr_demo.c | ||
LAST REVISION : October 4, 2002 | ||
SUBJECT : Program to demonstrate thread attributes. | ||
PROGRAMMER : (C) Copyright 2002 by Peter Chapin | ||
Please send comments or bug reports to | ||
Peter Chapin | ||
P.O. Box 317 | ||
Randolph Center, VT 05061 | ||
[email protected] | ||
****************************************************************************/ | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <pthread.h> | ||
|
||
pthread_mutex_t lock; // Synchronizes access to shared_data. | ||
int shared_data; // Imagine that this is much more complicated. | ||
|
||
|
||
// This is the background thread function. | ||
void *thread_function(void *arg) | ||
{ | ||
pthread_mutex_lock(&lock); | ||
printf("I'm in the thread function.\n"); | ||
|
||
// I use the shared data here. | ||
sleep(10); | ||
|
||
pthread_mutex_unlock(&lock); | ||
return NULL; | ||
} | ||
|
||
|
||
// Main function sets up the thread and configures a number of attributes. | ||
int main(void) | ||
{ | ||
pthread_attr_t attributes; // Thread attributes object. | ||
pthread_t threadID; // Identifier for the subordinate thread. | ||
void *result; // Return value from the subordinate thread. | ||
int rc; // Return code from thread functions. | ||
|
||
pthread_mutex_init(&lock, NULL); | ||
|
||
// Set up the attribute object according to our desires. | ||
pthread_attr_init(&attributes); | ||
rc = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED); | ||
if (rc != 0) printf("pthread_attr_setinheritsched() failed!\n"); | ||
rc = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO); | ||
if (rc != 0) printf("pthread_attr_setschedpolicy() failed!\n"); | ||
rc = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_PROCESS); | ||
if (rc != 0) printf("pthread_attr_setscope() failed!\n"); | ||
|
||
// Create the thread. Note that it has FIFO scheduling. | ||
rc = pthread_create(&threadID, &attributes, thread_function, NULL); | ||
if (rc != 0) { | ||
printf("pthread_create() failed!\n"); | ||
switch (rc) { | ||
case EAGAIN: | ||
printf("Insufficient resources or resource limit reached.\n"); | ||
break; | ||
case EINVAL: | ||
printf("Invalid thread attributes.\n"); | ||
break; | ||
case ENOMEM: | ||
printf("Insufficient memory.\n"); | ||
break; | ||
case EPERM: | ||
printf("Insufficient permission to create thread with specified attributes.\n"); | ||
break; | ||
} | ||
return 1; | ||
} | ||
|
||
printf("Subordinate thread created. Waiting...\n"); | ||
|
||
// Wait for it to end. | ||
pthread_join(threadID, &result); | ||
printf("Thread ended.\n"); | ||
|
||
pthread_mutex_destroy(&lock); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/**************************************************************************** | ||
FILE : barrier.c | ||
SUBJECT : Implementation of the barrier abstract type. | ||
PROGRAMMER : (C) Copyright 2010 by Peter C. Chapin <[email protected]> | ||
****************************************************************************/ | ||
|
||
#include "barrier.h" | ||
|
||
void barrier_init( barrier_t *b, int limit ) | ||
{ | ||
pthread_mutex_init( &b->lock, NULL ); | ||
pthread_cond_init( &b->not_enough, NULL ); | ||
pthread_cond_init( &b->all_released, NULL ); | ||
if( limit < 1 ) limit = 1; | ||
b->max = limit; | ||
b->count = 0; | ||
b->releasing = 0; | ||
b->wait_needed = 0; | ||
} | ||
|
||
void barrier_destroy( barrier_t *b ) | ||
{ | ||
pthread_cond_destroy( &b->not_enough ); | ||
pthread_cond_destroy( &b->all_released ); | ||
pthread_mutex_destroy( &b->lock ); | ||
} | ||
|
||
void barrier_wait( barrier_t *b ) | ||
{ | ||
pthread_mutex_lock( &b->lock ); | ||
|
||
// If the previous batch of threads is releasing, wait until they are all released. | ||
while( b->releasing ) pthread_cond_wait( &b->all_released, &b->lock ); | ||
|
||
// One more thread on the barrier. | ||
++b->count; | ||
|
||
// If we've reached the limit, start the releasing process. | ||
if( b->count == b->max ) { | ||
b->releasing = 1; | ||
b->wait_needed = 0; | ||
pthread_cond_broadcast( &b->not_enough ); | ||
--b->count; | ||
} | ||
else { | ||
// We are not at the limit; we need to wait. | ||
b->wait_needed = 1; | ||
while( b->wait_needed ) pthread_cond_wait( &b->not_enough, &b->lock ); | ||
--b->count; | ||
|
||
// If we are the last thread out, turn off the releasing process and let others in. | ||
if( b->count == 0 ) { | ||
b->releasing = 0; | ||
pthread_cond_broadcast( &b->all_released ); | ||
} | ||
} | ||
pthread_mutex_unlock( &b->lock ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/**************************************************************************** | ||
FILE : barrier.h | ||
SUBJECT : Interface to the barrier abstract data type. | ||
PROGRAMMER : (C) Copyright 2010 by Peter C. Chapin <[email protected]> | ||
****************************************************************************/ | ||
|
||
#ifndef BARRIER_H | ||
#define BARRIER_H | ||
|
||
#include <pthread.h> | ||
|
||
typedef struct { | ||
pthread_mutex_t lock; | ||
pthread_cond_t all_released; | ||
pthread_cond_t not_enough; | ||
int max; | ||
int count; | ||
int releasing; | ||
int wait_needed; | ||
} barrier_t; | ||
|
||
void barrier_init( barrier_t *b, int limit ); | ||
void barrier_destroy( barrier_t *b ); | ||
void barrier_wait( barrier_t *b ); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/bin/bash | ||
|
||
# Check for executable files. | ||
if [ ! -f bfish -o ! -x bfish ]; then | ||
echo "Error: can't locate executable file 'bfish'" | ||
exit 1 | ||
fi | ||
|
||
if [ ! -f bfishmt -o ! -x bfishmt ]; then | ||
echo "Error: can't locate executable file 'bfishmt'" | ||
exit 1 | ||
fi | ||
|
||
# Check for test files. | ||
if [ -f afile.txt -o -f afile.enc -o -f afile.out ]; then | ||
echo "Warning: Test file: afile.txt, afile.enc, or afile.out exists" | ||
echo "Overwrite? " | ||
read RESPONSE junk | ||
if [ $RESPONSE = 'n' ]; then | ||
exit 1 | ||
fi | ||
rm -f afile.txt afile.enc afile.out | ||
fi | ||
|
||
# Should search the path for makebig. Maybe later | ||
|
||
# Perform tests. | ||
ERROR=0 | ||
for SIZE in 2048 4096 6144 8192 10250; do | ||
echo "Size = $SIZE" | ||
makebig -b$SIZE > afile.txt 2> /dev/null | ||
|
||
echo "bfish..." | ||
bfish -e afile.txt afile.enc "Hello, World" | ||
bfish -d afile.enc afile.out "Hello, World" | ||
cmp afile.txt afile.out | ||
if [ $? -ne 0 ]; then | ||
ERROR=1 | ||
fi | ||
|
||
echo "bfishmt..." | ||
bfishmt -e afile.txt afile.enc "Hello, World" | ||
bfishmt -d afile.enc afile.out "Hello, World" | ||
cmp afile.txt afile.out | ||
if [ $? -ne 0 ]; then | ||
ERROR=1 | ||
fi | ||
|
||
echo "bfish to bfishmt..." | ||
bfish -e afile.txt afile.enc "Hello, World" | ||
bfishmt -d afile.enc afile.out "Hello, World" | ||
cmp afile.txt afile.out | ||
if [ $? -ne 0 ]; then | ||
ERROR=1 | ||
fi | ||
|
||
echo "bfishmt to bfish..." | ||
bfishmt -e afile.txt afile.enc "Hello, World" | ||
bfish -d afile.enc afile.out "Hello, World" | ||
cmp afile.txt afile.out | ||
if [ $? -ne 0 ]; then | ||
ERROR=1 | ||
fi | ||
|
||
done | ||
|
||
# Clean up. | ||
rm -f afile.txt afile.enc afile.out | ||
|
||
if [ $ERROR -eq 1 ]; then | ||
echo 'FAIL!' | ||
else | ||
echo 'PASS!' | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/**************************************************************************** | ||
FILE : bfish.c | ||
LAST REVISION : 2008-09-15 | ||
SUBJECT : File encryption program using Blowfish. | ||
PROGRAMMER : (C) Copyright 2008 by Peter C. Chapin | ||
This program allows the user to encrypt or decryption a file using the | ||
Blowfish algorithm. | ||
To Do: | ||
+ Consider using a better way to convert pass phrases into keys. | ||
Please send comments or bug reports to | ||
Peter C. Chapin | ||
Computer Information Systems | ||
Vermont Technical College | ||
Randolph Center, VT 05061 | ||
[email protected] | ||
****************************************************************************/ | ||
|
||
// Standard | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
// Unix specific | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
|
||
// OpenSSL | ||
#include <openssl/blowfish.h> | ||
|
||
#define BUFFER_SIZE 4096 | ||
|
||
extern int optind; | ||
|
||
char buffer[BUFFER_SIZE]; | ||
int count; | ||
|
||
// ============ | ||
// Main Program | ||
// ============ | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int option; | ||
int do_encrypt = 0; | ||
int do_decrypt = 0; | ||
int direction; | ||
int in; // Input file handle. | ||
int out; // Output file handle. | ||
unsigned char raw_key[16]; | ||
BF_KEY key; | ||
unsigned char IV[8]; | ||
int IV_index; | ||
|
||
while ((option = getopt(argc, argv, "ed")) != -1) { | ||
switch (option) { | ||
case 'e': do_encrypt = 1; direction = BF_ENCRYPT; break; | ||
case 'd': do_decrypt = 1; direction = BF_DECRYPT; break; | ||
} | ||
} | ||
|
||
if (argc - optind != 3) { | ||
fprintf(stderr, | ||
"Usage: %s -e|-d infile outfile \"pass phrase\"\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
if ((do_encrypt == 1 && do_decrypt == 1) || | ||
(do_encrypt == 0 && do_decrypt == 0)) { | ||
fprintf(stderr, "Exactly one of -e or -d must be specified.\n"); | ||
return 1; | ||
} | ||
|
||
// Prepare the key. | ||
strncpy(raw_key, argv[optind + 2], 16); | ||
BF_set_key(&key, 16, raw_key); | ||
|
||
// Prepare the IV. | ||
memset(IV, 0, 8); | ||
IV_index = 0; | ||
|
||
// Open the files. | ||
if ((in = open(argv[optind + 0], O_RDONLY)) == -1) { | ||
perror("Error opening input file"); | ||
return 1; | ||
} | ||
if ((out = open(argv[optind + 1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) { | ||
perror("Error opening output file"); | ||
close(in); | ||
return 1; | ||
} | ||
|
||
while ((count = read(in, buffer, BUFFER_SIZE)) > 0) { | ||
BF_cfb64_encrypt(buffer, buffer, count, &key, IV, &IV_index, direction); | ||
if (write(out, buffer, count) != count) { | ||
perror("Error writing output"); | ||
break; | ||
} | ||
} | ||
|
||
close(in); | ||
close(out); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.