LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-08-2008, 10:52 PM   #1
sandeep0424
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
Unhappy 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;

// printf("%d\n",num);
gen_rand(4);
//d=mix(1,0,1);

for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%d",messgx[i][j]);
}
printf("\n");
}

hash(messgx);
printf("\n the hashes are\n");
for(i=0;i<8;i++)
{

printf("%d\n",abs(hashes1[i]));
}

for(i=0;i<num;i++)
{
flag[i]=0;
Fork Gen(i);
}
for(j=1;j<8;j++)
{
printf("Authentication path %d is",j);
for(k=0;k<num;k++)
{
while(flag[k]==0)
wait;
printf("%d",AP[k]);
flag[k]=flag[k]-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


And the function waits here...........

Please help me.......
 
Old 12-09-2008, 08:59 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by sandeep0424 View Post
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.
 
Old 12-09-2008, 03:15 PM   #3
sandeep0424
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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);
}
}

void gen_rand(int n)
{
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 f=8;
//num=log2(f)-1;
//printf("enter the value of num");
//scanf("%d",&num);
//num=log2(f)-1;

// printf("%d\n",num);
gen_rand(4);
//d=mix(1,0,1);

for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%d",messgx[i][j]);
}
printf("\n");
}

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");
Gen(i);
}
for(j=1;j<8;j++)
{
printf("Authentication path %d is",j);
for(k=0;k<num;k++)
{
while(flag[k]==0)
sleep(10);//pid = wait(&status);//wait;
printf("%d",AP[k]);
flag[k]=flag[k]-1;
}
}

return 0;
}

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.....

After compiling and running,it gives-

sg431@pluto> gcc new3.c -lm
sg431@pluto> ./a.out
10111100
11010110
00001011
00011110
00111010
11110100
10101001
00011101

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

x8 = 1297581520
x9 = 0
x4 = 0
x8 = 1338391305
x9 = -6015360
x5 = 6015360

after the while loop of Emit

x7= 8

after first emit

and becomes still here..............

Last edited by sandeep0424; 12-09-2008 at 03:16 PM. Reason: changes in code
 
Old 12-09-2008, 04:14 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by sandeep0424 View Post
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.
 
Old 12-09-2008, 04:36 PM   #5
teknik
Member
 
Registered: Jun 2006
Location: Winnipeg, Canada
Distribution: Slackware 12.1
Posts: 33

Rep: Reputation: 16
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...

while (flag[a]!=0)
sleep(1);
 
Old 12-09-2008, 11:50 PM   #6
sandeep0424
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Unhappy

Ok..i am writing my program with comments:

#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;

// printf("%d\n",num);
gen_rand(4);
//d=mix(1,0,1);

for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%d",messgx[i][j]);
}
printf("\n");
}

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

for(k=0;k<num;k++)
{
while(flag[k]==0)
wait(&status); //sleep(10);//pid = wait(&status);//wait;
printf("%d",AP[k]);
flag[k]=flag[k]-1;
}
}
}


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..........
 
Old 12-10-2008, 06:10 AM   #7
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
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.)

-KD
 
Old 12-10-2008, 09:26 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by sandeep0424 View Post
Ok..i am writing my program with comments:

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC giving warning - braces around initializers fcdev Programming 4 09-28-2007 02:21 AM
How Do I Run/Compile a Program Written in C? debsan Fedora 2 05-25-2006 03:58 PM
Apache giving 'mod_throttle.c is already added' warning? sk545 Linux - Software 3 11-08-2004 01:05 PM
have you ever written a program for linux? today53 General 33 12-05-2003 07:54 PM
another dumb program written in bash. micxz Programming 0 10-13-2003 08:23 PM


All times are GMT -5. The time now is 04:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration