Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <string.h>
- #include <math.h>
- pthread_t *tid;
- int **numbers;
- int thread_count;
- int width;
- int height;
- ///gcc -o test main.c -lpthread
- /*
- void* by_number(void *arg){
- pthread_t id = pthread_self();
- for(int x = 0; x < thread_count; x++){
- if(pthread_equal(id,tid[x])){
- for(int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- if(can_change(x, numbers[i][j]) == 1){
- numbers[i][j] = 255 - numbers[i][j];
- }
- }
- }
- }
- }
- }
- */
- void* by_number(void *arg){
- pthread_t id = pthread_self();
- for(int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- for(int x = 0; x < thread_count; x++){
- if(pthread_equal(id,tid[x])){
- if(can_change(x, numbers[i][j]) == 1){
- numbers[i][j] = 255 - numbers[i][j];
- }
- }
- }
- }
- }
- }
- int can_change(int thread_num, int number){
- int step = 256 / thread_count;
- if(thread_num * step <= number && number < (thread_num + 1) * step) return 1;
- return 0;
- }
- void* by_block(void *arg){
- pthread_t id = pthread_self();
- for(int x = 0; x < thread_count; x++){
- if(pthread_equal(id,tid[x])){
- for(int i = 0; i < height; i++){
- int j_start = (x) * (ceil(width / thread_count) + 1);
- int j_stop = (x + 1) * (ceil(width / thread_count) + 1) - 1;
- for(j_start; j_start <= j_stop; j_start++){
- numbers[i][j_start] = 255 - numbers[i][j_start];
- }
- }
- }
- }
- }
- int main(int argc, char const *argv[])
- {
- thread_count = atoi(argv[1]);
- int mode = atoi(argv[2]); //mode 1 - numbers, mode 2 - blocks
- char *file_path = argv[3];
- char *out_name = argv[4];
- FILE *f = fopen(file_path, "r");
- if(f == NULL){
- printf("failed to open the first file");
- return 0;
- }
- FILE *out = fopen(out_name, "w+");
- if(out == NULL){
- printf("failed to open the first file");
- return 0;
- }
- tid = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
- int bufferLength = 255;
- char buffer[bufferLength];
- for(int i = 0; i < 3; i++){
- fgets(buffer, bufferLength, f);
- fputs(buffer, out);
- }
- char *pch = strtok(buffer," ");
- width = atoi(pch);
- pch = strtok (NULL, " ");
- height = atoi(pch);
- numbers = (int **)malloc(height * sizeof(int *));
- for (int i = 0; i < width; i++){
- numbers[i] = (int *)malloc(width * sizeof(int));
- }
- for(int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- numbers[i][j] = i + j;
- }
- }
- fgets(buffer, bufferLength, f);
- fputs(buffer, out);
- pch = NULL;
- int number;
- for(int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- fscanf(f, "%d", &number);
- numbers[i][j] = number;
- }
- }
- for(int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- printf("%i ", numbers[i][j]);
- }
- printf("\n");
- }
- printf("\n");
- if(mode == 1){
- for(int i = 0; i < thread_count; i++){
- pthread_create(&(tid[i]), NULL, &by_block, NULL);
- }
- }
- if(mode == 2){
- for(int i = 0; i < thread_count; i++){
- pthread_create(&(tid[i]), NULL, &by_number, NULL);
- }
- }
- sleep(1);
- for(int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- fprintf(out, "%d ", numbers[i][j]);
- printf("%i ", numbers[i][j]);
- }
- fprintf(out, "\n");
- printf("\n");
- }
- printf("%i\n", can_change(0, 3));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement