使用posix线程的c minimax
我正在用POSIX线程编写程序,以查找整数数组中的minimax。我的问题是如何确定需要多少个线程,以及在线程函数中输出最小值和最大值是否正确?为什么我需要动态线程 这是我的密码:使用posix线程的c minimax,c,dynamic,pthreads,posix,minimax,C,Dynamic,Pthreads,Posix,Minimax,我正在用POSIX线程编写程序,以查找整数数组中的minimax。我的问题是如何确定需要多少个线程,以及在线程函数中输出最小值和最大值是否正确?为什么我需要动态线程 这是我的密码: #include<pthread.h> #include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<sys/wait.h> #define num_of_threads 1 #defin
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#define num_of_threads 1
#define size 10
int array[size];
typedef struct st_minimax {
int min;
int max;
} minimax;
void *thread_minimax(void* ptr) { //thread is searching
int i;
minimax *data;
// data = (minimax*) malloc(minimax);
data = (minimax*) ptr;
data->max = array[0];
data->min = array[0];
for (i = 0; i < size; i++) {
if (array[i] > data->max)
data->max = array[i];
else if (array[i] < data->min)
data->min = array[i];
}
printf("Thread is calculating...\n");
printf("Min: %d", data->min);
printf("Max: %d", data->max);
pthread_exit(NULL); //exit thread
}
int main(int argc, char *argv[]) {
pthread_t worker[num_of_threads];
minimax data;
int t;
int i;
int ret;
for (i = 0; i < size; i++) { //initialize the array
printf("enter value for %d: ", i);
scanf("%d", &array[i]);
}
for(t=0;t<num_of_threads;t++)
{
printf("creating threads...%d\n",t);
ret = pthread_create(&worker[t], 0, thread_minimax, (void*) &data); //creating thread
sleep(1);
if (ret) {
fprintf(stderr, "thread err %d", ret);
exit(-1);
}
}
pthread_exit(NULL); //exit thread
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义\u线程的数量\u 1
#定义尺寸10
int数组[大小];
typedef结构st_minimax{
int-min;
int max;
}极小极大;
void*thread_minimax(void*ptr){//线程正在搜索
int i;
极小极大*数据;
//数据=(minimax*)malloc(minimax);
数据=(minimax*)ptr;
数据->最大值=数组[0];
数据->最小值=数组[0];
对于(i=0;i数据->最大值)
数据->最大值=数组[i];
else if(数组[i]min)
数据->最小值=数组[i];
}
printf(“线程正在计算…\n”);
printf(“最小值:%d”,数据->最小值);
printf(“最大值:%d”,数据->最大值);
pthread_exit(NULL);//退出线程
}
int main(int argc,char*argv[]){
pthread_t worker[线程数];
极小极大数据;
int t;
int i;
int ret;
对于(i=0;i您需要指定您的条件,以确定“需要”的线程数。正如目前编写的,您只需要1个,因为线程扫描整个阵列
一个线程从一个线程打印是可以的;但是,如果您试图找到单个数组的最小值和最大值,并在多个线程之间分解工作,那么您可能不想这样做(相反,让主进程处理每个线程的结果以获得总体答案)
不确定关于动态数组和动态线程的问题是什么;main可以动态地为每个数组分配空间(类似于在线程中注释掉的代码中所做的);在线程中什么需要更动态
您需要指定您的条件,以确定“需要”的线程数。正如目前编写的,您只需要1个,因为线程扫描整个阵列
一个线程从一个线程打印是可以的;但是,如果您试图找到单个数组的最小值和最大值,并在多个线程之间分解工作,那么您可能不想这样做(相反,让主进程处理每个线程的结果以获得总体答案)
不确定关于动态数组和动态线程的问题是什么;main可以动态地为每个数组分配空间(类似于在线程中注释掉的代码中所做的);在线程中什么需要更动态
我如何确定需要多少线程
这个问题的本质不需要任何多线程编程
在线程函数中输出最小值和最大值是否正确
这没什么错。它完全基于你需要什么(如果是作业
,它完全取决于你的教授想要什么!)
我如何修改程序来分配动态数组和动态线程
要动态分配数组,可以使用malloc()
,要释放数组,可以使用free()
,如下所示
int *ptr = NULL;
if ((ptr = (int *) malloc(sizeof(int) * len)) == NULL) {
printf("unable to allocate memory \n");
return -1;
}
/* you can use the ptr to read-from/write-to the allocated memory */
free(ptr);
您所说的动态线程是什么意思
OTOH,当我执行你的代码片段时,我没有发现任何错误。它工作得很好
我如何确定需要多少线程
这个问题的本质不需要任何多线程编程
在线程函数中输出最小值和最大值是否正确
这没什么错。它完全基于你需要什么(如果是作业
,它完全取决于你的教授想要什么!)
我如何修改程序来分配动态数组和动态线程
要动态分配数组,可以使用malloc()
,要释放数组,可以使用free()
,如下所示
int *ptr = NULL;
if ((ptr = (int *) malloc(sizeof(int) * len)) == NULL) {
printf("unable to allocate memory \n");
return -1;
}
/* you can use the ptr to read-from/write-to the allocated memory */
free(ptr);
您所说的动态线程是什么意思
OTOH,当我执行您的代码片段时,我没有发现任何错误。它运行得很好!根据与作者的对话,N
(1d)数组应该使用M
元素(即N
行和M
列)创建如果存在N
数组,则应创建N
线程(对于数组[0],线程-0)。线程应在相应数组上工作,并获取该数组的min
和max
值
下面是实现这一点的代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* N is the number of threads */
/* also N is the number of arrays */
/* row */
#define N 10
/* M is the length of each array */
/* column */
#define M 10
/* the 2d array which contains N rows and M columns */
int **matrix;
void display(void)
{
int i, j;
for (i=0 ; i<N ; i++) {
for (j=0 ; j<M ; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
return;
}
void * thread_handler(void *thread_id)
{
int *id = (int *) thread_id;
int i = *id, j;
int min = matrix[i][0];
int max = matrix[i][0];
for (j=1 ; j<M ; j++) {
if (matrix[i][j] > max)
max = matrix[i][j];
if (matrix[i][j] < min)
min = matrix[i][j];
}
printf("array[%d] min[%d] max[%d]\n", i, min, max);
pthread_exit(NULL);
}
int main(void)
{
int i, j, ret;
pthread_t tids[N];
int ids[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
srand(time(NULL));
matrix = (int **) malloc (sizeof(int *) * N);
for (i=0 ; i<N ; i++)
matrix[i] = (int *) malloc (sizeof(int) * M);
for (i=0 ; i<N ; i++) {
for (j=0 ; j<M ; j++) {
matrix[i][j] = rand() % 500;
}
}
display();
for (i=0 ; i<N ; i++) {
printf("Creating thread #%d \n", ids[i]);
ret = pthread_create(&tids[i], NULL, thread_handler, &ids[i]);
if (ret) {
printf("unable to create thread! \n");
exit(-1);
}
}
for (i=0 ; i<N ; i++) {
pthread_join(tids[i], NULL);
}
pthread_exit(NULL);
for (i=0 ; i<N ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
希望这有帮助!根据与作者的对话,N
(1d)数组应该用M
元素(即N
行和M
列)创建,如果有N
数组,那么应该创建N
线程(对于数组[0],线程-0)。线程应在相应的数组上工作,并获取该数组的min
和max
值
下面是实现这一点的代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* N is the number of threads */
/* also N is the number of arrays */
/* row */
#define N 10
/* M is the length of each array */
/* column */
#define M 10
/* the 2d array which contains N rows and M columns */
int **matrix;
void display(void)
{
int i, j;
for (i=0 ; i<N ; i++) {
for (j=0 ; j<M ; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
return;
}
void * thread_handler(void *thread_id)
{
int *id = (int *) thread_id;
int i = *id, j;
int min = matrix[i][0];
int max = matrix[i][0];
for (j=1 ; j<M ; j++) {
if (matrix[i][j] > max)
max = matrix[i][j];
if (matrix[i][j] < min)
min = matrix[i][j];
}
printf("array[%d] min[%d] max[%d]\n", i, min, max);
pthread_exit(NULL);
}
int main(void)
{
int i, j, ret;
pthread_t tids[N];
int ids[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
srand(time(NULL));
matrix = (int **) malloc (sizeof(int *) * N);
for (i=0 ; i<N ; i++)
matrix[i] = (int *) malloc (sizeof(int) * M);
for (i=0 ; i<N ; i++) {
for (j=0 ; j<M ; j++) {
matrix[i][j] = rand() % 500;
}
}
display();
for (i=0 ; i<N ; i++) {
printf("Creating thread #%d \n", ids[i]);
ret = pthread_create(&tids[i], NULL, thread_handler, &ids[i]);
if (ret) {
printf("unable to create thread! \n");
exit(-1);
}
}
for (i=0 ; i<N ; i++) {
pthread_join(tids[i], NULL);
}
pthread_exit(NULL);
for (i=0 ; i<N ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
希望这有帮助!例如,我需要在3个不同的数组中找到最小-最大值,因此我应该在main中输出最小-最大值?如果数组的计算是独立的(从其他注释来看,它们是独立的),您可以从线程中输出结果。虽然如果从main中输出,您可以更轻松地控制生成结果的顺序。例如,我需要在3个不同的数组中找到最小最大值,因此如果数组的计算是独立的,我应该在main中输出最小最大值(从其他注释来看,听起来像是这样),您可以从线程输出结果。虽然如果您从main执行此操作,您可以更轻松地控制结果的生成顺序