Multiple co-processor file descriptors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple co-processor file descriptors
# 8  
Old 03-02-2005
vger99 has the syntax. But the direction you are taking seems to be wrong. A fd is specific to a process. Exporting a vaiable containing an fd is senseless. In your first post, when you said "function", I assumed you meant "function". A function is ksh concept and it can have locally scoped variables. A global variable is global to the script, not the environment.
# 9  
Old 03-02-2005
Quote:
Originally Posted by Perderabo
vger99 has the syntax. But the direction you are taking seems to be wrong. A fd is specific to a process. Exporting a vaiable containing an fd is senseless. In your first post, when you said "function", I assumed you meant "function". A function is ksh concept and it can have locally scoped variables. A global variable is global to the script, not the environment.
First of all, vger99, thank you. This will solve the problem that I was struggling with. I was probably wrapping myself around my own axils pretty tight because I did double up the "$" symbols, however, I was escaping the second rather than the first. Thanks again.

Secondly, Perderabo, I don't really prefer the direction I am taking either but, considering the fact that I am trying to fit this into my existing library of functions, I think that this may be acceptable. I am also considering an array (three arrays perhaps because of KSH-88) that will help me find the fds.

I'll explain the concept further to clear it up; perhaps you have a better suggestion. Judging from the examples that I have seen with your name attached, I am sure you can come up with something more elegant.

a. I have existing functions to communicate to my co-process, lets call them func_write and func_read.
b. Within these functions, I currently communicate to my co-process with print -p and read -p.
c. I want to eliminate the need to disconnect from my current database so I can communication to a new database for some sporadic lookups.
d. I thought that I could create two co-processes (hence requiring the I/O redirection) and define file descriptors for each co-process.
e. Therefore, to keep my current functions generic, I thought that I would supply a database descriptor (handle) to each function (e.g. func_write "something" database_handle_1)
f. Within the function, I would translate "database_handle_1" into a variable that was previously exported so that I can retrieve the correct fd and then use print -u${database_handle_1_stdout}

Arrays will give me the same thing but referencing dynamic variables seemed logial when I started thinking through how to implement this.

Thanks again,

Thomas
# 10  
Old 03-02-2005
In item "f"; why do you suddenly start talking about exported variables? export makes a variable available to child processes. Those children will have their own fd's. Take at look at this:
Code:
#! /usr/bin/ksh

X=2

function one
{
        typeset X
        X=7
        echo in one X = $X
        return
}

function two
{
        X=9
        echo in two X = $X
        return
}
echo before one X = $X
one
echo before two X = $X
two
echo final X = $X
exit 0

Note that there is no "export". However there is a global variable called X. The function called "two" uses this global variable. The function called "one", while it uses a variable called X, it is a local X, not the global X.

When it run it, I get:
before one X = 2
in one X = 7
before two X = 2
in two X = 9
final X = 9
# 11  
Old 03-02-2005
Quote:
Originally Posted by Perderabo
In item "f"; why do you suddenly start talking about exported variables? export makes a variable available to child processes. Those children will have their own fd's.
Thanks for pointing that out. I understand the scope of the variables with regards to the functions and exporting the variables is overkill; you are correct. I really want global variables; my thought process was clouded by my "eval" issues.

I have a (now) working proof of concept but I would still like to hear your thoughts on improving the concept.

Code:
#!/usr/bin/ksh

function connect
{
    typeset x=$1
    typeset o=$2
    typeset i=$3

    printf "\nFunction connect()\n"

    sqlplus -s /nolog |&

    eval "${x}_stdout=${o}"
    eval "${x}_stdin=${i}"

    STDOUT=$(eval "print \$${x}_stdout")
    STDIN=$(eval "print \$${x}_stdin")

    eval "exec ${STDOUT}>&p"
    eval "exec ${STDIN}<&p"

    if [ ${x} = "TEST1" ]
    then
        eval "print -u${STDOUT} \"connect USER_A/PWD_A@DB1\""
    else
        eval "print -u${STDOUT} \"connect USER_B/PWD_B@DB2\""
    fi
    eval "read -u${STDIN} LINE"
    print "From sqlplus: $LINE"
}

function test_comm
{
    typeset x=$1

    printf "\nFunction test_comm()\n"

    STDOUT=$(eval "print \$${x}_stdout")
    STDIN=$(eval "print \$${x}_stdin")

    eval "print -u${STDOUT} \"SHOW USER\""
    eval "read -u${STDIN} LINE"
    print "From sqlplus: $LINE"
}

ps -ef | grep sqlplus | grep -v grep
connect TEST1 3 4
connect TEST2 5 6

ps -ef | grep sqlplus | grep -v grep
test_comm TEST1
test_comm TEST2
test_comm TEST1
test_comm TEST2

Now here are the results:

Function connect()
From sqlplus: Connected.

Function connect()
From sqlplus: Connected.

me 8118 8106 0 13:51:00 pts/6 0:00 sqlplus -s /nolog
me 8113 8106 0 13:51:00 pts/6 0:00 sqlplus -s /nolog

Function test_comm()
From sqlplus: USER is "USER_A"

Function test_comm()
From sqlplus: USER is "USER_B"

Function test_comm()
From sqlplus: USER is "USER_A"

Function test_comm()
From sqlplus: USER is "USER_B"

The results are what I want and I will use some other kind of global variable to track and define fds so "o" and "i" in connect() won't really exist when implemented.

Thanks again,

Thomas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions about file descriptors

Hi, I'm playing with KSH I entered following command in terminal { echo "stdout" >&1; echo "stderr" >&2; } > out And I get only stoud in a new file out. My question is: Where did my stderr vanish ? (5 Replies)
Discussion started by: solaris_user
5 Replies

2. UNIX for Dummies Questions & Answers

Semaphores and File Descriptors

What is the difference between a file descriptor and a semaphore? My basic understanding is: - a file descriptor is a small positive integer that the system uses instead of the file name to identify an open file or socket. - a semaphore is a variable with a value that indicates the... (1 Reply)
Discussion started by: Mr_Webster
1 Replies

3. HP-UX

exec and file descriptors

Hi, I speak and write english more or less, so I hope my asking be clear. :) In the company I am working, they are using control-m software to lunch shell scripts. So i put this command in all shell scripts: export LOGFILE_tmp=$PRODUC_DATA/tmp/${SCRIPT}_${PAIS}_`date... (0 Replies)
Discussion started by: anamcara
0 Replies

4. UNIX for Dummies Questions & Answers

Write/read to file descriptors

Is it possible to write to file descriptor 0 and read from 1 or 2? How could this be implemented? (3 Replies)
Discussion started by: machshev
3 Replies

5. UNIX for Advanced & Expert Users

File Descriptors + cron

Hi All, This thread is going to be a discussion basically bringing out more information from the experts on cron jobs and the associated file handles. So, here is the question. There is definitely a constant ' n ' as the maximum number of file handles alloted to a process ' p '. Will... (7 Replies)
Discussion started by: matrixmadhan
7 Replies

6. UNIX for Advanced & Expert Users

File descriptors missing on startup

Dec 20 15:34:32 hostname sendmail: File descriptors missing on startup: stderr; Bad file number Dec 20 15:34:32 hostname sendmail: File descriptors missing on startup: stderr; Bad file number Dec 20 15:34:32 hostname sendmail: File descriptors missing on startup: stderr; Bad file number Dec... (1 Reply)
Discussion started by: xnightcrawl
1 Replies

7. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

8. UNIX for Dummies Questions & Answers

file descriptors

i m trying to learn processes in unix and i've been reading this but i don't quite get it. its regarding file descriptors. : each is a part of file pointers, they point to another area. indexes into an Operating system maintained table called "file descriptor table". one table per process. may... (3 Replies)
Discussion started by: a25khan
3 Replies

9. UNIX for Advanced & Expert Users

File Descriptors

Hello all, A few questions on file descriptors ... scenario : Sun Ultra 30 with Sun OS 5.5.1 , E250 with Solaris 2.6 In one of my servers, the file descriptor status from the soft limit and hard limits are 64 and 1024 respectively for root user. Is the soft limit (64) represents the... (3 Replies)
Discussion started by: shibz
3 Replies

10. Programming

File Descriptors

Hi, I have written a daemon process, to perform certain operations in the background. For this I have to close, the open file descriptors, Does anybody know how to find out the number of open file descriptors ? Thanks in Advance, Sheetal (2 Replies)
Discussion started by: s_chordia
2 Replies
Login or Register to Ask a Question