1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/*
* source_00.c
*
* Copyright 2017 ss <ss@ss-system>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
* use in linux terminal:
*
* 1. $ gcc -o source_00 source_00.c -lpthread
* 2. $ ./source_00 3
* 3. ls
* time
* gcc -version
*/
/*
*
* Include necessary library
*
*
*
*/
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 36
#define MAX 36
char matrix[6][6]; //result sheet
//queue elements
char queue[MAX], head=0, tail=0;
int row=0,col=0;
//track the number of times we have run the dispatch funciton
int runtime=1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/*
*
* name: dispatch
* @param: void
* @return: void
*
*/
void dispatch(void){
row = runtime / 6;
sleep(1); //wait 1 second to generate column number
col = runtime % 6 - 1;
if (runtime % 6 == 0)
col = 5;
}
/*
*
* name: enqueue
* @param: character c
* @return: void
*
*/
void enqueue(char c){
queue[tail] = c;
tail = (tail+1)%MAX ;
}
/*
*
* name: dequeue
* @param: void
* @return: character temp
*
*/
char dequeue(){
char temp = queue[head];
head = (head+1)%MAX ;
return temp;
}
/*
*
* name: qcTask
* @param: void * args
* @return: void
* description: master thread
*/
void *qcTask(void *args)
{
//simulate product convey delay
sleep(1);
pthread_mutex_lock(&mutex);
dispatch();
sleep(rand()%6+5);
if(rand()%100<90){
enqueue('Q');
}
else{
enqueue('U');
}
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
/*
*
* name: main
* @param: int argc, char* argv[]
* @return: int, no return value
* description: program entry piont
*/
int main (int argc, char* argv[])
{
//printf("This\n");
if(argv[1] == NULL){
printf("argv is NULL!\n");
}
printf("Enter some linux terminal command: \n");
int input = atoi(argv[1]);/// converts string to integer
//printf( "This\n");
if(argc!=2 || input == 0 || input>36){
printf("Input is illegal!\n");
}
else{
pthread_t threads[input];
//printf( "This\n");
int rc, i, count = 0;
srand(time(NULL));
//printf( "This\n");
for(i=0;i<input;i++){
rc = pthread_create(&threads[i], NULL, qcTask, NULL);
if (rc){
printf("Error:unable to create thread%d\n",rc);
//cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
//printf( "This\n");
for (i = 0; i < input; i++){
//printf( "This p\n");
rc = pthread_join(threads[i], NULL);
if (rc){
printf( "Error:unable to join,%d\n",rc);
exit(-1);
}
}
//initize matrix with default value 'i'
//printf( "This\n");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
matrix[i][j] = 'I';
}
}
//fill the result sheet
for(int i=0;i<6;i++){
for (int j = 0; j < 6; j++){
matrix[i][j]=dequeue();
}
}
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
printf( "%d",matrix[i][j]);
if (matrix[i][j] == 'U'){
count++;
}
}
printf("\n");
}
printf("products are unqualified.\n");
pthread_exit(NULL);
}
/// not reach to this piont
printf( "End of Program Execution\n");
}
|