I have written a program and it is giving me the following warning
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have written a program and it is giving me the following warning
The program is running but it is becoming still(i think is due to the wait function).Could you guys please help me in this matter.
I think i have to use fork function,but dont know how and whre to use it.
The program is :
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
//#include <iostream.h>
//#include<conio.h>
#include<math.h>
//#define MAX 100
int messgx[9][9],i,j,k,d=0,flag[8];
int hashes1[8],hashes2[8],len,len1;
int f=8;
// int num = log2(f)-1;
#define num 100
int AP[num];
void gen_rand(int n);
//int mix(int a, int b, int c);
int hash (int messgx[][8]);
void Gen(int a);
int Emit(int a,int value);
int H(int c,int b);
int Onewfun(int d,int e);
int MakeY(int d);
void gen_rand(int n) /* returns random number in range of 0 to 99 */
{
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
int HIGH=1,LOW=0;
messgx[i][j]=rand() % (HIGH - LOW + 1) + LOW;
; /* n is random number in range of 0 - 99 */
}
}
}
void Gen(int a)
{
int y=pow(2,(a+1));
int z=pow(2,a);
printf("%d",y);
for(j=0;j<8;j+=y)
{
Emit(a,H((j+z),(j+y-1)));
Emit(a,H(j,(j+z-1)));
}
}
int Emit(int a,int value)
{
while (flag[i]!=0)
wait;
AP[i]=value;
flag[i]=pow(2,i);
return 0;
}
int H(int c,int b)
{
if(c == b)
return(Onewfun(c,MakeY(c)));
else
return(hash(Onewfun(H(c,((c+b-1)/2)),H(((c+b+1)/2),b))));
}
int Onewfun(int d,int e)
{
int g=17;
int p=log(d);
int q=log(e);
int h=(p/q);
h=h/g;
return h;
}
int MakeY(int d)
{
int s=hashes1[d];
return s;
}
int hash (int messgx[][8])
{
//unsigned char *p = hash;
int h = 0;
//int i;
for ( i = 0; i < 8; i++ )
{
for(j=0;j<8;j++)
{
h += messgx[i][j];
h += ( h << 10 );
h ^= ( h >> 6 );
}
h += ( h << 3 );
h ^= ( h >> 11 );
h += ( h << 15 );
hashes1[i]=h;
}
return 0;
}
int main()
{
//int f=8;
//num=log2(f)-1;
//printf("enter the value of num");
//scanf("%d",&num);
//num=log2(f)-1;
After compiling,
sg431@pluto> gcc -Wall -pedantic testr.c -o math -lm -std=c99
testr.c: In function âEmitâ:
testr.c:58: warning: statement with no effect
testr.c: In function âHâ:
testr.c:69: warning: passing argument 1 of âhashâ makes pointer from integer without a cast
testr.c: In function âmainâ:
testr.c:134: warning: passing argument 1 of âhashâ from incompatible pointer type
testr.c:145: error: âForkâ undeclared (first use in this function)
testr.c:145: error: (Each undeclared identifier is reported only once
testr.c:145: error: for each function it appears in.)
testr.c:145: error: syntax error before âGenâ
testr.c:153: warning: statement with no effect
After it runs,
sg431@pluto> ./math
10111100
11010110
00001011
00011110
00111010
11110100
10101001
00011101
the hashes are
1338391305
1459515613
2048485976
143501575
132349303
326355971
189791192
994772466
The program is running but it is becoming still(i think is due to the wait function).Could you guys please help me in this matter.
I think i have to use fork function,but dont know how and whre to use it.
After it runs,
sg431@pluto> ./math
10111100
11010110
00001011
00011110
00111010
11110100
10101001
00011101
the hashes are
1338391305
1459515613
2048485976
143501575
132349303
326355971
189791192
994772466
What is your program supposed to do? There's one comment in the whole program. I'm not going to compile/run it, to debug your code...if this is your program, you need to be the one to fix it. You know what it does, and what you want it to do.
If you want to know more about the fork function, Google can give you lots of C examples, and there are plenty of books that have it too.
sorry for not explaining what my program does....i am new to this org.....
actually the program has to just pass through all the functions and print the values stored in AP array.I have now removed the fork and the wait function,so it is running quite a good...but its still becoming still after coming to a specific point.
here's the modified program......please help me to run this...pleaseeeeeeeeeeeee.....
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include <errno.h>
//#include <iostream.h>
//#include<conio.h>
#include<math.h>
//#define MAX 100
int messgx[8][8],i,j,k,d=0,flag[8];
int hashes1[8];
#define num 4
int AP[num];
void Gen(int a);
void gen_rand(int n);
int hash (int messgx[][8]);
int Emit(int a,int value);
int H(int c,int b);
int MakeY(int d);
int Onewfun(int d,int e);
int Emit(int a,int value)
{
//pid1 = getpid();
while (flag[a]!=0)
sleep(1);
printf("\n after the while loop of Emit \n");
AP[a]=value;
flag[a]=pow(2,a);
return 8;
}
int MakeY(int d)
{
int s=hashes1[d];
return s;
}
int Onewfun(int d,int e)
{
int g=17;
int p=log(d);
int q=log(e);
int h=(p/q);
h=h/g;
return h;
}
int H(int c,int b)
{
if(c == b)
{
int x8=abs(MakeY(c));
printf("\n x8 = %d",x8);
int x9=Onewfun(c,x8);
printf("\n x9 = %d",x9);
return x9;
}
else
return 6;
}
void Gen(int a)
{
int w=(a+1);
int y=pow(2,w);
int z=pow(2,a);
printf("\n in Gen %d",z);
for(j=0;j<8;j+=y)
{
int x1=(j+z);
int x2=(j+y-1);
printf("x2=%d\n",x2);
int x3=(j+z-1);
int x4=H(x1,x2);
printf("\n x4 = %d",x4);
int x5=abs(H(j,x3));
printf("\n x5 = %d\n",x5);
int x7=Emit(a,x4);
printf("\n x7= %d\n",x7);
printf("\n after first emit\n");
int x6=Emit(a,x5);
printf("\n x6=%d\n",x6);
}
}
The program just selects 8 random bit messages(each of 8 bits) in rand_gen() and then computes its hash value in hash().Then it passes through the functions Emit(),Onewayf(),MakeY() and computes the value of AP[] array and then prints it.
I just need the program to run.....please help me.....
the hashes are
in the absol
1338391305
in the absol
1297581520
in the absol
1466408390
in the absol
1351908065
in the absol
105089028
in the absol
1447741359
in the absol
134503636
in the absol
367340487
this is 1this is i
in Gen 1x2=1
sorry for not explaining what my program does....i am new to this org.....
actually the program has to just pass through all the functions and print the values stored in AP array.I have now removed the fork and the wait function,so it is running quite a good...but its still becoming still after coming to a specific point.
here's the modified program......please help me to run this...pleaseeeeeeeeeeeee.....
after the while loop of Emit
x7= 8
after first emit
and becomes still here..............
To repost what I said earlier:
There's one comment in the whole program. I'm not going to compile/run it, to debug your code...if this is your program, you need to be the one to fix it. You know what it does, and what you want it to do.
You can't just come and drop a 200 line program, containing mostly math, with absolutely no comments, and expect someone to be able to look at it and instantly know what your problem is. It's a lot harder than "Please fix this". As TB0ne said, this is your program, and you know what it is supposed to do. The best way to figure out why it isn't working correctly is to run through it manually on a piece of paper. Go through it exactly how the computer would do it.
However, that being said, you said something about hanging in Emit? You have a while loop... be sure sure that flag[a] is being set to 0 some time...
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include <errno.h>
//#include <iostream.h>
//#include<conio.h>
#include<math.h>
//#define MAX 100
int messgx[8][8],i,j,k,d=0,flag[8];
int hashes1[8];
#define num 4
int AP[num],status;
int Gen(int a);
void gen_rand(int n);
int hash (int messgx[][8]);
int Emit(int a,int value);
int H(int c,int b);
int MakeY(int d);
int Onewfun(int d,int e);
int Emit(int a,int value)
{
//pid1 = getpid();
while (flag[a]!=0) //after flag[i]=1;the child process waits and allows the parent process to execute
wait(&status);
printf("\n after the while loop of Emit \n");
AP[a]=value;
flag[a]=pow(2,a); //here it makes its flag[i] as 1
printf("\n For in emit flag: %d\n",flag[a]);
return 8;
}
int MakeY(int d)
{
int s=hashes1[d];
return s;
}
int Onewfun(int d,int e)
{
int g=17;
d=abs(d*g);
e=abs(e*g);
int h = (d*e);
/*int p=log(d);
int q=log(e);
int h=(p/q);*/
h=h/g;
return h;
}
int H(int c,int b)
{
if(c == b)
{
int x8=abs(MakeY(c));
printf("\n x8 = %d",x8);
int x9=Onewfun(c,x8);//printf("in if loop of H");//return(Onewfun(c,MakeY(c)));
printf("\n x9 = %d",x9);
return x9;
}
else
return 6;//printf("\n in else loop of H");//return(Onewfun(H(c,((c+b-1)/2)),H(((c+b+1)/2),b)));
}
int Gen(int a)
{
int w=(a+1);
int y=pow(2,w);
int z=pow(2,a);
printf("\n in Gen %d",z);
for(j=0;j<8;j+=y)
{
int x1=(j+z);
int x2=(j+y-1);
printf("x2=%d\n",x2);
int x3=(j+z-1);
int x4=H(x1,x2);
printf("\n x4 = %d",x4);
int x5=abs(H(j,x3));
printf("\n x5 = %d\n",x5);
int x7 = Emit(a,x4); //jumps to Emit function with flag[i]=0
printf("\n x7= %d\n",x7);
printf("\n after first emit\n");
int x6=Emit(a,x5);//Emit(a,H(j,(j+z-1)));
printf("\n x6=%d\n",x6);
}
return 20;
}
void gen_rand(int n) /* returns random number in range of 0 to 99 */
{
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
int HIGH=1,LOW=0;
messgx[i][j]=rand() % (HIGH - LOW + 1) + LOW;
; /* n is random number in range of 0 - 99 */
}
}
}
int hash (int messgx[][8])
{
//int **per = addr;
//unsigned char *p = hash;
int h = 0;
//int i;
for ( i = 0; i < 8; i++ )
{
for(j=0;j<8;j++)
{
h += messgx[i][j];
h += ( h << 10 );
h ^= ( h >> 6 );
}
h += ( h << 3 );
h ^= ( h >> 11 );
h += ( h << 15 );
hashes1[i]=h;
}
return h;
}
int main()
{
int fin;
//int f=8;
//num=log2(f)-1;
//printf("enter the value of num");
//scanf("%d",&num);
//num=log2(f)-1;
hash(messgx);
printf("\n the hashes are\n");
for(i=0;i<=7;i++)
{
printf("in the absol\n");
printf("%d\n",abs(hashes1[i]));
}
printf("this is 1");
for(i=0;i<4;i++)
{
flag[i]=0;
printf("this is i");
fork(); //forks the process
Gen(i); // first child process starts executing from here with flag[i]=0
}
printf("\n after gen ,fin value: %d\n",fin);
for(j=1;j<8;j++)
{
printf("Authentication path %d is",j); //here the parent process prints the AP values when flag[i]!=0
and child process waits
the process is as follows:
first flag[i]=0,child process starts and executes Gen[i],then to Emit() where its flag[i]=1.With that the child process waits and now the aprent process should starts executing printing the AP values(code at the end of program)
The second child starts after the parent process prints AP values and again the same process continues with this time 2 AP values will be printed
the for 3rd child, 4 AP values..........
It will be good if you can remove the the unneccessary lines of codes (I mean the commented lines of the code).So that it will easier for someone to debug. try to put some comments to highlight what the function or what a particular block is doing.
Please put all those, and after that though you are the right person to debug it, still others can try (if the logic what u want to imply is clear to others.)
the process is as follows:
first flag[i]=0,child process starts and executes Gen[i],then to Emit() where its flag[i]=1.With that the child process waits and now the aprent process should starts executing printing the AP values(code at the end of program)
The second child starts after the parent process prints AP values and again the same process continues with this time 2 AP values will be printed
the for 3rd child, 4 AP values..........
@sandeep0424, are you reading the comments left for you? Teknik left you a solution, and I left notes on how YOU are the person who needs to debug/fix your own code, as did kauuttt.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.